473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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('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
0 3087

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jinming Xu | last post by:
Hello Everyone, I am trying to write a python script to telnet to a server and then do something there. As the first step, I practiced the python example in Lib Reference 11.13.2. But I am...
6
by: Richard Bird CCNP, CCDP, MCSE, etc. | last post by:
I need some help adding multithreading to an existing python script. The script was developed to telnet and make changes on a list of cisco routers. I figure that by adding multithreading, I'd be...
2
by: newsf | last post by:
Hi, I want to make a program what connect through telnet protocol to a router, and run commands for configure the router. I read over sockets and I made the program above, but it donīt work fine....
3
by: Lidvar | last post by:
Hi. I wonder if anyone know a easy way to control a ISDN router in telnet, in visual basic..?
2
by: _andrea.l | last post by:
I'm writing a little script for reading information from a router by telnet. I have a problem How give a comand and get the answer more times? I means: $usenet = fsockopen($cfgServer,...
2
by: vmalhotra | last post by:
Hi I am new in python scripting. I want to open a Multiple telnet session through once script. In other way i can tell i want to open two linux consoles through one script. I wrote one...
3
by: svjames | last post by:
Hey guys, im working on a project that requires a telnet connection to be made from a web page to a router, auto login then quit/drop whatever, its just gotta run one and in the background. my...
17
by: ravimath | last post by:
Dear all, I have written following script to loin to router bu it is showing error. #!c:\Perl\bin; use strict; use warnings; my $hostname = 'REMOVED FOR YOUR PROTECTION'; my $password =...
2
by: manuitpro | last post by:
Hi, I have implemented a php script by which i can login to a router using fsockopen(), and i can run any cisco commands. But i have requiremnt login to a remote from from a central router for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.