problem with stream (telnet to router)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mario.lat_

    problem with stream (telnet to router)

    Hallo to all,
    I have write a little script for connecting to cisco router BUT I have a
    problem: I have to send to router all the commands and then I have to read
    the output.
    If I send a command1 and read the output for command1 the script works
    well,
    If I sent command1 , I send command2 and then I read output for command1
    and command2 it works well.
    If i send command1 and then I read the output for command1 then send
    command2 I can't read the output for the command 2.
    Why?
    How can I do?
    Thank you in advance,
    Mario.

    P.S. following the script and the password



    =============== =============== == SCRIPT =============== =============== ==
    <?
    include "router.php ";

    $r=new Router();

    $r->connect('route r_address', $errno, $errstr);

    $r->login($myLogin ,$myPassword);


    $o=$r->runCommand('sh o ver'); //<==== works
    echo '<pre>';
    var_dump($o);
    echo '</pre>';

    $o=$r->runCommand('sh o int descr'); //<==== don't works

    $r->disconnect() ;

    ?>



    =============== =============== ====== Class =============== =============== ============

    <?

    Class Router
    {

    function Router()
    {
    $this->connesso=0;
    }

    function connect($Server Address, $errno, $errstr, $cfgPort=23,$cf gTimeOut=10)
    {
    $this->stream= fsockopen($Serv erAddress, $cfgPort, $errno, $errstr, $cfgTimeOut);
    if(!$this->stream)
    {
    $this->connesso=0;
    }
    else
    {
    $this->connesso=1;
    }
    }

    function disconnect()
    {
    if($this->connesso==0){e xit();}
    fwrite ($this->stream, "lo\r\n");
    fclose($this->stream);
    $this->connesso=0;
    }

    function login ($login,$passwo rd)
    {
    if($this->connesso==0){e xit();}

    if(strlen($logi n)>0)
    {
    fputs ($this->stream, "$login\r\n ");
    fputs ($this->stream, "$password\r\n" );
    }
    else
    {
    fputs ($this->stream, "$password\r\n" );
    }

    stream_set_time out($this->stream, 2);

    fputs ($this->stream,"termin al length 0\r\n");
    /*
    if I send a list of commands here it get the command and i see the output
    in next read of strream, for example:
    fputs ($this->stream,"sho int desc\r\n"); fputs
    ($this->stream,"sho ver\r\n");
    ....
    */

    }


    function runCommand($com mand,$logoutput =0,$ntimeout=10 )
    {

    if(strlen($comm and)>0)
    {
    fputs ($this->stream, $command."\r\n" );
    }


    $output=array() ;
    while(!feof($th is->stream))
    {
    $info = stream_get_meta _data($this->stream);
    if ($info['timed_out'])
    {
    $ntimeout--;
    usleep(100000);
    }
    else
    {
    $line=fread($th is->stream,1000) ;
    $output[]=$line;
    }
    if ($ntimeout==0)
    {
    break;
    }
    }
    return $output;
    }



    }//class Router
    ?>
Working...