Connecting Tech Pros Worldwide Help | Site Map

Establish a connection telnet in PHP on a UNIX server

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:57 AM
Robert PADOVANO
Guest
 
Posts: n/a
Default Establish a connection telnet in PHP on a UNIX server

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: rpadovano@buyingpack.com

Robert PADOVANO






  #2  
Old July 17th, 2005, 04:57 AM
Daniel Tryba
Guest
 
Posts: n/a
Default Re: Establish a connection telnet in PHP on a UNIX server

Robert PADOVANO <rpadovano@buyingpack.com> wrote:
[fsockopen][color=blue]
> This solution works correctly but it not stable
> - I can't modify the timeout which remains hopelessly fixed to 30s[/color]

http://php.net/pfsockopen
resource pfsockopen ( string hostname, int port [, int errno [, string
errstr [, int timeout]]])

Take a closer look at the last parameter :)
[color=blue]
> Is somebody already wrote a functional program in PHP with the sockets for
> telnet?[/color]

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.