niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
· nixdoc.net · man pages · Linux HOWTOs · FreeBSD Tips · Forums
navigation Forum index » Programming » PHP
Telnet Response via PHP Sockets
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
Author Message
sithellaneous@gmail.com
*nix forums beginner


Joined: 27 May 2006
Posts: 10

PostPosted: Thu Jul 13, 2006 1:33 am    Post subject: Telnet Response via PHP Sockets Reply with quote

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 Sad).

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 top
Bent Stigsen
*nix forums Guru Wannabe


Joined: 07 Mar 2005
Posts: 113

PostPosted: Sat Jul 15, 2006 2:12 pm    Post subject: Re: Telnet Response via PHP Sockets Reply with quote

b00x wrote:
[snip]
Quote:
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.


Quote:
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.


Quote:
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 Sad).

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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Thu Sep 09, 2010 5:59 am | All times are GMT
navigation Forum index » Programming » PHP
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Server high ping response & sometimes timeouts chrislo2004 Postfix 0 Thu Jul 29, 2010 2:45 am
No new posts I can't telnet into the courier MTA service. bluethundr Postfix 0 Tue Apr 13, 2010 7:50 pm
No new posts returning 550 response code Micha Silver Postfix 2 Wed Jul 19, 2006 1:36 pm
No new posts Sockets noreply PHP 1 Wed Jul 19, 2006 5:36 am
No new posts Not able to telnet Ramesh D Solaris 1 Mon Jul 17, 2006 1:21 pm

Copyright © 2004-2005 DeniX Solutions SRL
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum |  email marketing service
 
Sponsors: Breast Enlargement | DVD rental | Lockers | Breast Enlargement | Debt Help
Privacy Policy
[ Time: 0.0860s ][ Queries: 17 (0.0555s) ][ GZIP on - Debug on ]