By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,712 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,712 IT Pros & Developers. It's quick & easy.

problem with stream (telnet to router)

P: n/a
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('router_address', $errno, $errstr);

$r->login($myLogin,$myPassword);
$o=$r->runCommand('sho ver'); //<==== works
echo '<pre>';
var_dump($o);
echo '</pre>';

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

$r->disconnect();

?>

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

<?

Class Router
{

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

function connect($ServerAddress, $errno, $errstr, $cfgPort=23,$cfgTimeOut=10)
{
$this->stream= fsockopen($ServerAddress, $cfgPort, $errno, $errstr, $cfgTimeOut);
if(!$this->stream)
{
$this->connesso=0;
}
else
{
$this->connesso=1;
}
}

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

function login ($login,$password)
{
if($this->connesso==0){exit();}

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

stream_set_timeout($this->stream, 2);

fputs ($this->stream,"terminal 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($command,$logoutput=0,$ntimeout=10)
{

if(strlen($command)>0)
{
fputs ($this->stream, $command."\r\n");
}
$output=array();
while(!feof($this->stream))
{
$info = stream_get_meta_data($this->stream);
if ($info['timed_out'])
{
$ntimeout--;
usleep(100000);
}
else
{
$line=fread($this->stream,1000);
$output[]=$line;
}
if ($ntimeout==0)
{
break;
}
}
return $output;
}

}//class Router
?>
May 16 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.