473,387 Members | 1,504 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,387 software developers and data experts.

Telnet Response via PHP Sockets

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.d...a3bb4ff13e6433)

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.

Jul 13 '06 #1
2 15729
b00x wrote:
[snip]
function getResponse(&$socket) {

//Reading response
echo "Read response: ";

while ($buf = socket_read($socket, 2048)) {
//$output .= $buf;
echo $buf;
}
[snip]
// 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);
[snip]
The problem here, is that it doesn't make it past the first
socket_read(). I've even tried other methods using fread(), etc.
You are not exactly following protocol. Just sending some generic chunks of
commands with intermittent reads, might actually work, but I doubt it is
portable across different telnet servers. Note that telnet don't use
headers like http. It is a ping pong sequence of commands exchanged to
negotiate which options to use. One negotiation may lead to another, e.g.
if you say you are willing to tell what type of terminal you got, then the
server may subsequently ask for it. Nonetheless, it is still possible to do
what you do, but it is a bit clunky. I assume you didn't construct the
content of "$header1" and "$header2", so you may need to verify that all
the wills, wonts, dos and don'ts match how your telnet-server behaves.

Your present "getResponse" will not work. The break-condition for the
while-loop will only happen in case of timeout, socket error or if the
connection is closed in the remote end. When there is no data ready on a
(open) socket, the socket_read will wait until there is, unless it is set
to non-blocking, but you probably don't want that.

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.
You will have to deal with it, as it is normal behavior. I am not sure about
the 2 characters though, as I would expect a character of value 255 (IAC)
to be the first, but the "garbage" you are going to see in the beginning,
would presumably be negotiation commands as described in the telnet
protocol, either requests or responses to commands you send.

About the hang. As it ("getResponse") is, it will allways hang. In your
case, the short answer is simply not to read unless you expect something,
but I suspect that the only safe way to do that is to send, read and
respond in a proper manner according to protocol, until the server is
satisfied and sends you the (expected) login-prompt, which would be a cue
for you to stop reading. When you send username, you can expect a
password-prompt. When you send the correct password you can expect a
shell-prompt. As long as you are in sync with what is happening, you
shouldn't find yourself unnecessarily stuck on a read.

Things like setting the non-block option or using a short timeout can be
useful, but only for the purpose to not hang on a read, is messy. Say if
you didn't get something on the first read and have to read again because
you really was expecting something, perhaps sleep a while and then try once
again because you really really was expecting something, ... If you go down
that road, I think you'll eventually find out that hanging on a read is a
blessing, not a problem.

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.
PROOF??? O thou of little faith, wherefore didst thou doubt. Behold, for the
absence of faith in unix brings dire failure. Failure from whence one can
only cry out "unix, save me!". Praised be unix, its derivatives, and the
whole internet, hallelujah.
Well, in other words, I am going to be lazy and simply claim it would work.

If ssh is available on the servers, you could take a look at the ssh2
extension for PHP. Nice features and less hassle. There is apparently no
stable version, but the beta seemed to work fine on Linux(Fedora). I don't
know how well it works on windows though.
ref: http://www.php.net/manual/en/ref.ssh2.php
--
/Bent
Jul 15 '06 #2
Woah!

That's like the largest response I've ever gotten! Thanks :)

I think I'll need to spend some time reading the RFC's, rather than
going off what other people have make in the past, etc. I think most of
the code I've based this off is for a windows telnet client, or some
such. In which case, I think I'll look into the further details of Unix
telnet connections, and SSH aswell. SSH isn't an option (unfortunately)
for all the servers I need this to work on, but it will have a partial
role. Maybe I'll do some packet sniffing aswell, and watch the traffic
and connections to help further understand. Seems my trust in the
internet's resources has failed me this time - though it still has the
answer, though hidden more deeply.

As for nix - your right - ALL HAIL!

Thanks Bent, I'll be sure to keep this topic updated with progress,
etc.
Bent Stigsen wrote:
b00x wrote:
[snip]
function getResponse(&$socket) {

//Reading response
echo "Read response: ";

while ($buf = socket_read($socket, 2048)) {
//$output .= $buf;
echo $buf;
}
[snip]
// 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);
[snip]
The problem here, is that it doesn't make it past the first
socket_read(). I've even tried other methods using fread(), etc.

You are not exactly following protocol. Just sending some generic chunks of
commands with intermittent reads, might actually work, but I doubt it is
portable across different telnet servers. Note that telnet don't use
headers like http. It is a ping pong sequence of commands exchanged to
negotiate which options to use. One negotiation may lead to another, e.g.
if you say you are willing to tell what type of terminal you got, then the
server may subsequently ask for it. Nonetheless, it is still possible to do
what you do, but it is a bit clunky. I assume you didn't construct the
content of "$header1" and "$header2", so you may need to verify that all
the wills, wonts, dos and don'ts match how your telnet-server behaves.

Your present "getResponse" will not work. The break-condition for the
while-loop will only happen in case of timeout, socket error or if the
connection is closed in the remote end. When there is no data ready on a
(open) socket, the socket_read will wait until there is, unless it is set
to non-blocking, but you probably don't want that.

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.

You will have to deal with it, as it is normal behavior. I am not sure about
the 2 characters though, as I would expect a character of value 255 (IAC)
to be the first, but the "garbage" you are going to see in the beginning,
would presumably be negotiation commands as described in the telnet
protocol, either requests or responses to commands you send.

About the hang. As it ("getResponse") is, it will allways hang. In your
case, the short answer is simply not to read unless you expect something,
but I suspect that the only safe way to do that is to send, read and
respond in a proper manner according to protocol, until the server is
satisfied and sends you the (expected) login-prompt, which would be a cue
for you to stop reading. When you send username, you can expect a
password-prompt. When you send the correct password you can expect a
shell-prompt. As long as you are in sync with what is happening, you
shouldn't find yourself unnecessarily stuck on a read.

Things like setting the non-block option or using a short timeout can be
useful, but only for the purpose to not hang on a read, is messy. Say if
you didn't get something on the first read and have to read again because
you really was expecting something, perhaps sleep a while and then try once
again because you really really was expecting something, ... If you go down
that road, I think you'll eventually find out that hanging on a read is a
blessing, not a problem.

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.

PROOF??? O thou of little faith, wherefore didst thou doubt. Behold, for the
absence of faith in unix brings dire failure. Failure from whence one can
only cry out "unix, save me!". Praised be unix, its derivatives, and the
whole internet, hallelujah.
Well, in other words, I am going to be lazy and simply claim it would work.

If ssh is available on the servers, you could take a look at the ssh2
extension for PHP. Nice features and less hassle. There is apparently no
stable version, but the beta seemed to work fine on Linux(Fedora). I don't
know how well it works on windows though.
ref: http://www.php.net/manual/en/ref.ssh2.php
--
/Bent
Jul 30 '06 #3

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

Similar topics

1
by: Robert PADOVANO | last post by:
Hello, I wish to establish a connection in language PHP on a UNIX server by telnet. In fact, I would like : - Connect to server by sending IP adress, login, password - Send an order (example:...
5
by: Greg Martz | last post by:
I'd like to do the following in C# and prefer using tcpclient rather than raw sockets... Connect to a unix box Login run date +%H%M%S retrieve the response. That's it, nothing more. This...
7
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a...
3
by: derSchweiz | last post by:
Hi, Experimenting with sockets, and I think I got it half working. I have the following code: Private Sub socket_receive(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
3
by: jtagpgmr | last post by:
I need help coming up with a way to create a c# program to help me Telnet to a hostIP.. Any suggestions.. or a way to start..
0
by: goroth | last post by:
I am trying to create a telnet client that will connect to several of my network devices on campus and change settings on the devices. So far I can connect with the code below, but I can't seem to...
0
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am trying to read text from a Telnet site melvyl.ucop.edu. I run the following VB .NET 2003 code: Dim nstc As New Net.Sockets.TcpClient("melvyl.ucop.edu", 23) Dim nsns As...
5
by: pbd22 | last post by:
Hi. Anybody know of any good code examples out there on how to take a telnet command and parse it? Thanks!
2
by: marcf | last post by:
Hello Everyone! I have so nearly finished a mud client for a customer to the point where I was about to send them the source code. Out of luck I took the project home to try it on my personal PC...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.