SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Telnet script?

  1. #1
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Telnet script?

    Has anyone had success with a telnet script to be used to log into Cicsco Catalyst switches?

    I've tried using the following script:
    http://www.geckotribe.com/php-telnet/

    But i'm not having any success logging into the switch and doing a simple port query.

    In a futile attempt to troubleshoot the issue. i installed Etheral (packet analyzer) to view all my packets.

    I started a capture of me successfully telneting into a switch via my normal route (cmd.. telnet (switch name/IP) ).

    Then i ran the script and did a packet capture as well. Everything looks ok connecting to the cat. I see the response from the switch. It successfully logs in, but for some reason it wouldnt execute the command i inputted in to the script. Although i do see the response from the cat, showing the command line ready to accept responses.

    Does anyone have a functional telnet script that I could experiment with or has someone tried this and run into similar issues?

    Here's my simple script to sho the port status on a module:
    PHP Code:
    <?php
    require_once "PHPTelnet.php";

    echo 
    "Showing Status of Module 5<br>:";
    $telnet = new PHPTelnet();
    // if the first argument to Connect is blank,
    // PHPTelnet will connect to the local host via 127.0.0.1
    $result $telnet->Connect('xxx.xxx.xxx.xxx','username','pass');
    if (
    $result == 0) { 
    $telnet->DoCommand('sho port 5'$result);
    // NOTE: $result may contain newlines
    echo $result;
    $telnet->DoCommand('sho port 6'$result);
    // say Disconnect(0); to break the connection without explicitly logging out
    $telnet->Disconnect();
    }
    ?>

    If someone has access to a cisco switch, could they try this script and see if it works for them?

  2. #2
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We'll doing a little more in depth search I found another class, the original modified for cisco swithces, but its still not working. can someone help debug?

    PHP Code:
    <?php        
    class PHPCiscoTelnet {
            var 
    $fp=NULL;
            var 
    $loginprompt;

            var 
    $buffer = array();
            var 
    $dump 0;
            var 
    $endPrompt ">";
            
    /*
            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]) 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='127.0.0.1';

    if (
    strlen($ip)) {
                            if (
    $this->fp=fsockopen($ip,23)) {
                                    
    fputs($this->fp,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));

                                    
    fputs($this->fp,chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
                                    
    chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21));
                                    
    $this->GetResponse($r);
                                    
    $r=explode("\n",$r);
                                    
    $this->loginprompt=$r[count($r)-1];
                                    
    fputs($this->fp,"$user\r");
                                    
    fputs($this->fp,"$pass\r");
                                    if ((
    $r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1])) {
                                            
    $rv=3;
                                            
    $this->Disconnect();
                                    }
                            } else 
    $rv=1;
                    }
                    
    $this->GetResponseUntilPrompt($tmp);
                    return 
    $rv;
            }

    function 
    Disconnect($exit=1) {
                    if (
    $this->fp) {
                            if (
    $exitfputs($this->fp"\nexit");
                            
    fclose($this->fp);
                            
    $this->fp=NULL;
                    }
            }

    function 
    DoCommand($c) {
                    if (
    $this->fp) {
                            
    fputs($this->fp,"$c\n");
                            
    $this->GetResponseUntilPrompt($r);
                            
    $r=preg_replace("/\r/"""$r);
                            
    $r=preg_replace("/".$c."/"""$r);
                            
    $tab explode("\n"$r);
                            
    $this->buffer array_slice($tab1count($tab) - 2);
                            return 
    1;
                    }
                    return 
    0;
            }

    function 
    enable($pwd) {
                    
    fputs($this->fp"enable\n");
            
    fputs($this->fp$pwd "\n");
                    
    $this->endPrompt="#";
                    
    $this->GetResponseUntilPrompt($tmp);
            }

    function 
    GetResponse(&$r) {
                    
    $r='';
                    do {
                            
    $r.=fread($this->fp,1000);
                            
    $s=socket_get_status($this->fp);
                    } while (
    $s['unread_bytes']) ;
               if (
    $this->dump)
                print 
    $r."\n";

            }

    function 
    GetResponseUntilPrompt(&$r) {
            
    $r='';
            do {
                
    $r.=fread($this->fp,1000);
                
    $s=socket_get_status($this->fp);
                if (
    preg_match("/ --More-- /"$r)) {
                    
    $r preg_replace("/ --More-- /""MORE"$r);
                    
    fputs($this->fp" ");
                }
            } while (! 
    preg_match("/".$this->endPrompt."$/"$r));

                    
    $r=preg_replace("/".chr(8)."/"""$r);
                    
    $r=preg_replace("/MORE        /"""$r);
                    if (
    $this->dump)
                            print 
    $r."\n";
        }

        
    function 
    display() {
                 foreach (
    $this->buffer as $line) {
                                     print 
    $line."\n";
                }
            }
    }


    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I've tried your script with a slighty modified output. If I intentionally use a wrong password, this is my output on a Catalyst 3750, from my Ubuntu 6.10 host:

    Showing Status of Module 5
    :show vlan Password:
    I was aiming at the result of a 'show vlan'..

    If I use correct credentials, the script times out with no output.
    Does it happen to you as well?

    I'm currently at work and I don't really have the time to debug, but I'll check it out more closely when I get back home!

    Do tell, if you get it working!

  4. #4
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Nunyah View Post
    Hello,

    I've tried your script with a slighty modified output. If I intentionally use a wrong password, this is my output on a Catalyst 3750, from my Ubuntu 6.10 host:



    I was aiming at the result of a 'show vlan'..

    If I use correct credentials, the script times out with no output.
    Does it happen to you as well?

    I'm currently at work and I don't really have the time to debug, but I'll check it out more closely when I get back home!

    Do tell, if you get it working!

    Nope, i didnt have much luck getting it to work. I couldnt get any output. Im in the process of reading up on sockets and using them instead of the fsockopen function. Let me know if you make progress as well.. maybe i'll give this script another shot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •