473,732 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(&$s ocket) {

//Reading response
echo "Read response: ";

while ($buf = socket_read($so cket, 2048)) {
//$output .= $buf;
echo $buf;
}

echo $output."\n";

return $output;
}

// Create internet socket
$socket = socket_create(A F_INET, SOCK_STREAM, SOL_TCP);

// Set 30 second timeout
socket_set_opti on($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($s ocket, $header1, strlen($header1 ));
echo "Sent Header 1\n";
echo getResponse($so cket);

//Header 2
socket_write($s ocket, $header2, strlen($header2 ));
echo "Sent Header 2\n";
echo getResponse($so cket);

// Do more stuff here once its working...

socket_close($s ocket);
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 15780
b00x wrote:
[snip]
function getResponse(&$s ocket) {

//Reading response
echo "Read response: ";

while ($buf = socket_read($so cket, 2048)) {
//$output .= $buf;
echo $buf;
}
[snip]
// Send telnet Header 1
socket_write($s ocket, $header1, strlen($header1 ));
echo "Sent Header 1\n";
echo getResponse($so cket);

//Header 2
socket_write($s ocket, $header2, strlen($header2 ));
echo "Sent Header 2\n";
echo getResponse($so cket);
[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 "getRespons e" 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 ("getRespons e") 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(&$s ocket) {

//Reading response
echo "Read response: ";

while ($buf = socket_read($so cket, 2048)) {
//$output .= $buf;
echo $buf;
}
[snip]
// Send telnet Header 1
socket_write($s ocket, $header1, strlen($header1 ));
echo "Sent Header 1\n";
echo getResponse($so cket);

//Header 2
socket_write($s ocket, $header2, strlen($header2 ));
echo "Sent Header 2\n";
echo getResponse($so cket);
[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 "getRespons e" 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 ("getRespons e") 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
6277
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: LS - Al) - Receive the result in a string - Send an order (example: CD ..) - Receive the result in a string - Etc...
5
33869
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 shouldn't be too complicated, I thought... I have yet to find any examples of being able to do this.
7
14094
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 Telnet client. I just need to send a telnet request to a local IP address on a LAN issue a "c" then a "b" and stream back the text for internal use. The "c" changes sub-menus and the "b" is a switch to dump the status of a firewall. I need to issue...
3
1499
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 MyBase.Activated Dim bytesRec As Integer Dim i As Integer = 0 Dim data1 As Net.Sockets.Socket = cidServer.Accept() 'this line is
3
6689
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
3853
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 get the correct return data from the device. Sometimes I get about 100 bytes and other times I get 3000 bytes. I even put a thread sleep but that still give me different return data from different devices. I know I am going to have to use an...
0
979
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 Net.Sockets.NetworkStream = nstc.GetStream() If nsns.DataAvailable Then Dim ab(nstc.ReceiveBufferSize) As Byte nsns.Read(ab, 0, CInt(nstc.ReceiveBufferSize))
5
8786
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
1737
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 just to make sure. At work on my corporate network everything works perfectly but it seems once anyone uses the client on a direct high speed connection to the internet the incoming text seems to all get jumbled up! This is using...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.