Php to implement telnet connection, pass commands, get return values, and other functions!
Php to implement telnet connection, pass commands, get return values, and other functions!
- Error_reporting (-1 );
-
- Class Telnet {
- Var $ sock = NULL;
-
- Function telnet ($ host, $ port ){
- $ This-> sock = fsockopen ($ host, $ port );
- Socket_set_timeout ($ this-> sock, 2, 0 );
- }
-
- Function close (){
- If ($ this-> sock) fclose ($ this-> sock );
- $ This-> sock = NULL;
- }
-
- Function write ($ buffer ){
- $ Buffer = str_replace (chr (255), chr (255). chr (255), $ buffer );
- Fwrite ($ this-> sock, $ buffer );
- }
-
- Function getc (){
- Return fgetc ($ this-> sock );
- }
-
- Function read_till ($ what ){
- $ Buf = '';
- While (1 ){
- $ IAC = chr (1, 255 );
-
- $ DONT = chr (1, 254 );
- $ DO = chr (1, 253 );
-
- $ WONT = chr (1, 252 );
- $ WILL = chr (251 );
-
- $ TheNULL = chr (0 );
-
- $ C = $ this-> getc ();
-
- If ($ c = false) return $ buf;
- If ($ c ==$ theNULL ){
- Continue;
- }
-
- If ($ c = "1 "){
- Continue;
- }
-
- If ($ c! = $ IAC ){
- $ Buf. = $ c;
-
- If ($ what = (substr ($ buf, strlen ($ buf)-strlen ($ what )))){
- Return $ buf;
- }
- Else {
- Continue;
- }
- }
-
- $ C = $ this-> getc ();
-
- If ($ c ==$ IAC ){
- $ Buf. = $ c;
- }
- Else if ($ c ==$ DO) | ($ c ==$ DONT )){
- $ Opt = $ this-> getc ();
- // Echo "we wont". ord ($ opt). "\ n ";
- Fwrite ($ this-> sock, $ IAC. $ WONT. $ opt );
- }
- Elseif ($ c ==$ WILL) | ($ c ==$ WONT )){
- $ Opt = $ this-> getc ();
- // Echo "we dont". ord ($ opt). "\ n ";
- Fwrite ($ this-> sock, $ IAC. $ DONT. $ opt );
- }
- Else {
- // Echo "where are we? C = ". ord ($ c)." \ n ";
- }
- }
- }
- }
-
- /*
- $ Telnet = new telnet ("192.168.0.1", 23 );
- Echo $ telnet-> read_till ("login :");
- $ Telnet-> write ("kongxx \ r \ n ");
- Echo $ telnet-> read_till ("password :");
- $ Telnet-> write ("KONGXX \ r \ n ");
- Echo $ telnet-> read_till (":> ");
- $ Telnet-> write ("ls \ r \ n ");
- Echo $ telnet-> read_till (":> ");
- Echo $ telnet-> close ();
-
- */
|
And return values.