Telnet using PHP

OK, I've seen alot of examples on how to use php for telnet purposes... some work... and some don't... my main problem is that the php script hangs on any version of fgets() or read() no matter what I tried (obviously I haven't tried the correct way :0). I have read the manual when it comes to sockets and it still doesn't work the way I think it should.

Unlike most of the examples out there I don't want to use PHP to allow the user to telnet into a server. Instead, I want my PHP script to connect to a server, send a preformatted command ( such as 'ls') and record the results, and close the connection. After that, I want to echo the results back to the webpage.

So here is the logic that I've tried:
1. Creating Socket (using socket() or fsockopen())
2. Connecting to Server (using connect())
3. Sending Username (using write() or fputs())
4. Sending Password (using write() or fputs())
5. Sending 'ls' Command (using write() or fputs())
6. Retrieving Directory Listing (using read or fgets())
7. Disconnecting from server (using close() or fclose())
8. Echo directory listing as webpage (using echo())

My ultimate goal is to automate a search feature ('grep') with a simple parameter, and return the results. This would save so much time, as my fellow workers use telnet to grep many different servers to perform advanced tech support by searching logs and perform cable modem provisioning.

Any help would be appreciated.

Robert Knepp
Adelphia Communications
Knowledge Engineer
rkneppAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
superschlonzCommented:
You could use rsh for executing the commands on the other machines.

Another way is using expect with telnet to execute the commands.

You can use both of them with popen( "command" ).
0
 
superschlonzCommented:
If you use rsh you have to setup the other machines correctly (/etc/hosts.equiv, ~/.rhosts). Then you should be able to execute commands from a shell:

> rsh otherhost ls -la

And from php:

$pipe = popen( "rsh otherhost ls -la", "r" );
...read with fgets
0
 
curriCommented:
I don't know how to use sockets either (will probably try later :) but wanted to point you towards expect. It is a program/language designed to do things like that. Essentially you start a telnet program and send keys to it, just as if you were typing, and read from it.

I haven't used it under windows, but it works great under unix/linux. If you're on  a linux system do man expect.

Orlando
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
MoondancerCommented:
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.

PLEASE DO NOT AWARD THE POINTS TO ME.

____________________________________________



Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.

Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange

P.S.  REGARDING POINTS, Guidelines and more:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  
0
 
rkneppAuthor Commented:
I'll choose option #3 as I do believe that everyone who posted comments had valid points that I did look into.

The reason I didn't choose a comment for an answer is this:

Since Linux (and PHP) are open source, it's different between different users (making questions like mine hard to answer)... this being the case no one really knows whether anything will work or not... they just have guesses... this is deserving of points just not all of them...
0
 
rkneppAuthor Commented:
I'll actually accept my own answer:

PHP and sockets just don't work most of the time... maybe if they got socket_get_status to work correctly it would...

So I scrapped the whole idea and recoded SSH2... :-)
0
 
MoondancerCommented:
I've refunded your points for this question and moved it at zero points to our PAQ to close it.
Moondancer
Community Support Moderator @ Experts Exchange
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.