0

I am trying to implement one PHP GUI that first establishes an SSH connection to a server and then it opens a Telnet connection to one machine that want to send a command to reboot. I have already tried the commands on a CLI (The SSH connection, Telnet connection, and the reboot command through this Telnet session).

But in my implementation I think I am missing something while trying to send the command to the Telnet session. Can some one give me a clue ?

<?php
function rebootMachine() {

    $original_Timeout=ini_get('default_socket_timeout');
    ini_set('default_socket_timeout',10);
    /IP addresses of the Server and the Machine
    $server_ip = 10.0.0.1 ;
    $machine_ip  = 10.10.0.1;
    //Telnet port to the Machine
    $port_telnet = 49150;

    $connection=@ssh2_connect($server_ip,22);
    if(false==$connection){
        echo "Server is unreacheable";
    }else{
    //conection the the Server
        $auth=@ssh2_auth_password($connection, 'username','password');

        if(false==$auth){
            echo 'Authentication failed';
        }else{
            //Conection to the machine by Telnet 
            $stream=@ssh2_exec($connection, 'telnet machine_ip port_tcp');

            if (false==$stream){
                echo 'error';
            }else{
                //command to reboot the Machine
                $stream_telnet=exec(":SYSTEM:REBOOT");
            }
            fclose($stream);
        }
    }         
}
jww
  • 97,681
  • 90
  • 411
  • 885
Jim
  • 11
  • 2
  • 1
    ``exec()`` will execute the given command on your local computer, not the one you've Telnetted to. I think you need to ``use ssh2_exec()``. See https://www.php.net/manual/en/function.ssh2-exec.php – kmoser Nov 01 '19 at 03:57
  • No. Using "ssh2_exec($connection,":SYSTEM:REBOOT");" still does not work. It think that by appling this command I am just sending the command to the machine that I have connected through SSH ($server_ip) but not to the machine I've sent the telnet command ($machine_ip). – Jim Nov 06 '19 at 17:38
  • How do you know this for certain? It's just as possible that the Telnet user (i.e. the user you've logged in with via Telnet/SSH) doesn't have permission to reboot the system. Maybe try executing a command you know will work, e.g. ``ls >>~/foo.txt``, then checking whether ``foo.txt`` exists – kmoser Nov 08 '19 at 05:43
  • Also, this looks fishy: ``$stream=@ssh2_exec($connection, 'telnet machine_ip port_tcp');``. What are ``machine_ip`` and ``port_tcp``? You're also not using the ``$port_telnet`` variable. Is that intentional? – kmoser Nov 08 '19 at 05:46

0 Answers0