By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,824 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,824 IT Pros & Developers. It's quick & easy.

Establish a connection telnet in PHP on a UNIX server

P: n/a
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...

I am already at a advanced stage but I am not solved my problem completely.
I have actually 3 solutions:

Solution 1:
-----------
A program write with the sockets

//Function reading the flow
function lisflux($flux)
{
// waiting the first char
$Char = false;
while (!$Char) $Char = fgetc($flux);
$Output = $Char;

//Reading next chars
$Output. = fread($flux, 1024);

//Replace the carriage return
$Output = str_replace("\n", " ", $Output);
return $Output;
}
$Socket = pfsockopen($HostName, $Port);

if (! $Socket)
{
echo 'Connetion refused : <br>';
echo strerror ($Socket) . '<br>';
}
else
{
// Header 1
fputs($Socket, $Header1);
lisflux($Socket);

//Header 2
fputs($Socket, $Header2);
echo lisflux($Socket);

//Writing the login
fputs($Socket, "login\r");
echo lisflux($Socket);

//Writing Passwd
fputs($Socket, "passwd\r");
echo lisflux($Socket);

//Writing the order
fputs($Socket, #Commande. "\r");
echo lisflux($Socket);
}

This solution works correctly but it not stable
- I can't modify the timeout which remains hopelessly fixed to 30s
- The returned flow is sometimes incorrect.
In fact, I make 2 times the same thing but I do not obtaint the same
result.
Sometimes, returned flow is truncated. Sometimes, it is correct.

Is somebody already wrote a functional program in PHP with the sockets for
telnet?
Solution 2:
-----------
Use CURL which is originally implemented in PHP.
I wrote a program in PHP with CURL library.
This program runs correctly for a connection HTTP, ftp...
On the other hand, I have no documentation and no example for a connection
telnet

Is somebody already wrote a functional program in PHP with the CURL library
?

Solution 3:
-----------
Use EXPECT library
I have downloaded EXPECT for Windows (I develop with EasyPHP)
But the documentation is completely incompéhensible

Is somebody already wrote a functional program in PHP with the EXPECT
library ?
Thank you by advance to answer on my email: rp*******@buyingpack.com

Robert PADOVANO

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Robert PADOVANO <rp*******@buyingpack.com> wrote:
[fsockopen]
This solution works correctly but it not stable
- I can't modify the timeout which remains hopelessly fixed to 30s
http://php.net/pfsockopen
resource pfsockopen ( string hostname, int port [, int errno [, string
errstr [, int timeout]]])

Take a closer look at the last parameter :)
Is somebody already wrote a functional program in PHP with the sockets for
telnet?


You shouldn't be using telnet for this, this is what rsh (remote shell)
was introduced for. But even better maybe you can call an ssh
implementation on your platform (ssh or putty's plink).

--

Daniel Tryba

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.