Solved

Telnet using PHP

Posted on 2001-07-30
7
1,045 Views
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
0
Comment
Question by:rknepp
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:superschlonz
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" ).
0
 
LVL 3

Expert Comment

by:superschlonz
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
0
 
LVL 2

Expert Comment

by:curri
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.

Orlando
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:Moondancer
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.

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
 

Author Comment

by:rknepp
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...
0
 

Author Comment

by:rknepp
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... :-)
0
 
LVL 1

Accepted Solution

by:
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.
Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now