This article describes how to implement telnet in php. For more information, see
This article describes how to implement telnet in php. For more information, see
The Code is as follows:
Class PHPTelnet {
Var $ show_connect_error = 1;
Var $ use_usleep = 0; // change to 1 for faster execution
// Don't change to 1 on Windows servers unless you have PHP 5
Var $ sleeptime = 125000;
Var $ loginsleeptime = 1000000;
Var $ fp = NULL;
Var $ loginprompt;
Var $ conn1;
Var $ conn2;
/*
0 = success
1 = couldn't open network connection
2 = unknown host
3 = login failed
4 = PHP version too low
*/
Function Connect ($ server, $ user, $ pass ){
$ Rv = 0;
$ Vers = explode ('.', PHP_VERSION );
$ Needvers = array (4, 3, 0 );
$ J = count ($ vers );
$ K = count ($ needvers );
If ($ k <$ j) $ j = $ k;
For ($ I = 0; $ I <$ j; $ I ++ ){
If ($ vers [$ I] + 0)> $ needvers [$ I]) break;
If ($ vers [$ I] + 0) <$ needvers [$ I]) {
$ This-> ConnectError (4 );
Return 4;
}
}
$ This-> Disconnect ();
If (strlen ($ server )){
If (preg_match ('/[^ 0-9.]/', $ server )){
$ Ip = gethostbyname ($ server );
If ($ ip = $ server ){
$ Ip = '';
$ Rv = 2;
}
} Else $ ip = $ server;
} Else $ ip = '2017. 0.0.1 ';
If (strlen ($ ip )){
If ($ this-> fp = fsockopen ($ ip, 23 )){
Fputs ($ this-> fp, $ this-> conn1 );
$ This-> Sleep ();
Fputs ($ this-> fp, $ this-> conn2 );
$ This-> Sleep ();
$ This-> GetResponse ($ r );
$ R = explode ("\ n", $ r );
$ This-> loginprompt = $ r [count ($ r)-1];
Fputs ($ this-> fp, "$ user \ n ");
$ This-> Sleep ();
Fputs ($ this-> fp, "$ pass \ n ");
If ($ this-> use_usleep) usleep ($ this-> loginsleeptime );
Else sleep (1 );
$ This-> GetResponse ($ r );
$ R = explode ("\ n", $ r );
If ($ r [count ($ r)-1] = '') | ($ this-> loginprompt = $ r [count ($ r) -1]) {
$ Rv = 3;
$ This-> Disconnect ();
}
} Else $ rv = 1;
}
If ($ rv) $ this-> ConnectError ($ rv );
Return $ rv;
}
Function Disconnect ($ exit = 1 ){
If ($ this-> fp ){
If ($ exit) $ this-> DoCommand ('exit ', $ junk );
Fclose ($ this-> fp );
$ This-> fp = NULL;
}
}
Function DoCommand ($ c, & $ r ){
If ($ this-> fp ){
Fputs ($ this-> fp, "$ c \ n ");
$ This-> Sleep ();
$ This-> GetResponse ($ r );
$ R = preg_replace ("/^ .*? \ N (. *) \ n [^ \ n] * $/"," $1 ", $ r );
}
Return $ this-> fp? 1:0;
}
Function GetResponse (& $ r ){
$ R = '';
Do {
$ R. = freactive ($ this-> fp, 1024 );
$ S = socket_get_status ($ this-> fp );
} While ($ s ['unread _ bytes ']);
}
Function Sleep (){
If ($ this-> use_usleep) usleep ($ this-> sleeptime );
Else sleep (1 );
}
Function PHPTelnet (){
$ This-> conn1 = chr (0xFF). chr (0xFB). chr (0x1F). chr (0xFF). chr (0xFB ).
Chr (0x20). chr (0xFF). chr (0xFB). chr (0x18). chr (0xFF). chr (0xFB ).
Chr (0x27). chr (0xFF). chr (0xFD). chr (0x01). chr (0xFF). chr (0xFB ).
Chr (0x03). chr (0xFF). chr (0xFD). chr (0x03). chr (0xFF). chr (0xFC ).
Chr (0x23). chr (0xFF). chr (0xFC). chr (0x24). chr (0xFF). chr (0xFA ).
Chr (0x1F). chr (0x00). chr (0x50). chr (0x00). chr (0x18). chr (0xFF ).
Chr (0xF0). chr (0xFF). chr (0xFA). chr (0x20). chr (0x00). chr (0x33 ).
Chr (0x38). chr (0x34). chr (0x30). chr (0x30). chr (0x2C). chr (0x33 ).
Chr (0x38). chr (0x34). chr (0x30). chr (0x30). chr (0xFF). chr (0xF0 ).
Chr (0xFF). chr (0xFA). chr (0x27). chr (0x00). chr (0xFF). chr (0xF0 ).
Chr (0xFF). chr (0xFA). chr (0x18). chr (0x00). chr (0x58). chr (0x54 ).
Chr (0x45). chr (0x52). chr (0x4D). chr (0xFF). chr (0xF0 );
$ This-> conn2 = chr (0xFF). chr (0xFC). chr (0x01). chr (0xFF). chr (0xFC ).
Chr (0x22 ). chr (0xFF ). chr (0xFE ). chr (0x05 ). chr (0xFF ). chr (0xFC ). chr (0x21 );
}
Function ConnectError ($ num ){
If ($ this-> show_connect_error) switch ($ num ){
Case 1: echo'
[PHP Telnet] Connect failed: Unable to open network connection
'; Break;
Case 2: echo'
[PHP Telnet] Connect failed: Unknown host
'; Break;
Case 3: echo'
[PHP Telnet] Connect failed: Login failed
'; Break;
Case 4: echo'
[PHP Telnet] Connect failed: Your server \'s PHP version is too low for PHP Telnet
'; Break;
}
}
}
?>
,