|
Posted by b00x on 07/13/06 01:33
Hi,
I'm trying to develop a script that I can reuse to run remote commands
on multiple UNIX servers via telnet. I've tried various php scripts
after googling for a good 5 or so hours, but found no luck.
I've created a script (suprisingly) similar to the on found an old post
from 2001
(http://groups.google.com/group/php.dev/browse_thread/thread/b8814440cc65b51b/a5a3bb4ff13e6433?lnk=st&q=problem+telnet+read+response+php&rnum=2#a5a3bb4ff13e6433)
function getResponse(&$socket) {
//Reading response
echo "Read response: ";
while ($buf = socket_read($socket, 2048)) {
//$output .= $buf;
echo $buf;
}
echo $output."\n";
return $output;
}
// Create internet socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Set 30 second timeout
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" =>
30, "usec" => 0));
// Resolve hostname
$address = gethostbyname($host);
// Attempt connection
socket_connect($socket, $address, 23);
echo "Connected!\n";
// Send telnet Header 1
socket_write($socket, $header1, strlen($header1));
echo "Sent Header 1\n";
echo getResponse($socket);
//Header 2
socket_write($socket, $header2, strlen($header2));
echo "Sent Header 2\n";
echo getResponse($socket);
// Do more stuff here once its working...
socket_close($socket);
echo "Socket Closed\n";
echo $output;
The problem here, is that it doesn't make it past the first
socket_read(). I've even tried other methods using fread(), etc.
The same problem occurring is the response is only 2 characters (of
which are: ²$), and then the connection and script hang on the read
statement.
I've tried running this from a browser, from a command-line, but still
the same result. The only theory I have left, is that it has something
to do with the fact i'm running the script from a windows machine,
rather than a *nix one (This is because of availability issues :().
Can anybody suggest anything? Has anyone had the same problem? Simply
some proof that it works under *nix would be great.
Thanks.
Regards,
Corey
Notes: WinXP.Pro.SP2 - Apache/2.2.2 (Win32) PHP/5.1.4.
[Back to original message]
|