Establish a connection telnet in PHP on a UNIX server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert PADOVANO

    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($Hos tName, $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@buyin gpack.com

    Robert PADOVANO





  • Daniel Tryba

    #2
    Re: Establish a connection telnet in PHP on a UNIX server

    Robert PADOVANO <rpadovano@buyi ngpack.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]

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

    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

    Comment

    Working...