Click to See Complete Forum and Search --> : telnet using php


kloon_za
06-12-2008, 01:58 AM
Im trying to use php to connect to telnet server and run a command and echo it to screen. currently my code looks like this:

$fp = fsockopen("public-route-server.is.co.za", 23);
if(!$fp) {
echo "Failed to connect to local route server.";
} else {
while($buf=fgets($fp)) {
echo $buf."<br>";
}

fwrite($fp, "show ip bgp");
while($buf=fgets($fp)) {
echo $buf."<br>";
}
}

the first while is for the welcome message, and then it must write command and read the results. But for some reason, the script hangs at the first while, anybody perhaps know why?

sridhar_423
06-12-2008, 02:12 AM
Isn't length parameter required for fgets? Apart from this, I done see flaw in ur code.

kloon_za
06-12-2008, 02:13 AM
no length is optional, but i did try it with lenght in also.

sridhar_423
06-12-2008, 03:45 AM
I chkd the examples given on php.net for doing a telnet using fsockopen. Its not as simple as you've written.. atleast it seems to be like that for me. there are couple of examples over there which can be a good start for you

kloon_za
06-12-2008, 05:41 AM
I first tried those examples, they still timeout on me.

sridhar_423
06-12-2008, 06:00 AM
i never tried telnet before.. at the max. I executed scripts that are hosted on the same server... hence used shell_exec.
I'll try and will update in case if it works for me.

MrCoder
06-12-2008, 10:09 AM
$fp = fsockopen("public-route-server.is.co.za", 23);
if(!$fp) {
echo "Failed to connect to local route server.";
} else {
$buf=fgets($fp);
echo $buf."<br>";

fwrite($fp, "show ip bgp");

$buf=fgets($fp);
echo $buf."<br>";
}


Try that, fgets is a blocking function, execution will pause while the buffer is filled.

kloon_za
06-13-2008, 12:32 AM
Nah that doesnt work either, cause the welcome message is more than one line. I need it to try and read until the telnet server want a input, then i must send show ip bgp and the read until it ask for another input.