sockets with telnet
| From: | Gareth Thomas | Date: | Tue, 15 Jul 2003 20:16:04 +0000 |
| Subject: | sockets with telnet | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-20800@lists.php.net to get a copy of this message | ||
Hi,
I am trying to create a basic telnet client using sockets to connet to a
linux box. I have written some sockets code previously with a server script
that I developed myself and that worked fine. The problem I am getting is
with Telnet (port 23) after making the socket connection successfully all I
am able to read back is garbage. What I would like to do is go through an
automated login process into the Linux box, issue some OS commands and exit.
Does anyone have any ideas as to what the problem might be? Has anyone done
this successfully.
Here is my basic test code:
$socket = socket_create (AF_INET, SOCK_STREAM, getprotobyname("TCP"));
if ($socket < 0) {
echo "socket() failed: reason: " . socket_strerror (socket_last_error())
. "\n";
echo('failed');
} else {
"socket() successful: \n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "connect() failed.\nReason: ($result) " . socket_strerror($result)
. "\n";
} else {
echo "OK.\n";
}
if (!@socket_setopt($socket,SOL_SOCKET,SO_REUSEADDR,1)) {
echo "socket_setopt() failed: reason:
".socket_strerror(socket_last_error($sock))."\n";
exit;
}
$aresult = socket_bind( $socket, "192.168.0.80", 23 );
echo "Reading response:\n\n";
$out = socket_read ($socket, 2048,PHP_BINARY_READ);
echo $out;
echo "Closing socket...";
socket_close ($socket);