Telnet using PHP

Posted on 2001-07-30
Medium Priority
Last Modified: 2012-06-21
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
Question by:rknepp
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 6336345
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" ).

Expert Comment

ID: 6336387
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

Expert Comment

ID: 6337064
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.

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Expert Comment

ID: 6419221
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.



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  

Author Comment

ID: 6419473
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...

Author Comment

ID: 6419492
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... :-)

Accepted Solution

Moondancer earned 0 total points
ID: 6646190
I've refunded your points for this question and moved it at zero points to our PAQ to close it.
Community Support Moderator @ Experts Exchange

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question