0

I have got in my network topology:machine_1, server_1 and machine_2.

I am developing an application that will be hosted in machine_1. One of the functionalities of the application is to connect to the server_1 through a ssh connection (I have used ssh2_exec) and then the server_1 is able to reach machine_2 .

What I want to do is to send a telnet command inside this server_1 to the machine_2. As I have made the connection to the server_1 through the ssh2_exec command, is there a way to establish this telnet connection and then send the telnet command in PHP?

*obs: I am just able to send the command to the machine_2 through an established telnet connection from server_1.

<?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_2= 10.10.0.1;
$command =“ system:REBOOT”;
    //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_2 by Telnet 
            $stream=@ssh2_exec($connection, 'telnet $machine_2 $port_telnet');

            if (false==$stream){
                echo 'error';
            }else{
                //command to reboot the Machine

            }
            fclose($stream);
        }
    }         
}
Jim
  • 11
  • 2
  • Um, why cant you just run telnet as the `$command` in `ssh2_exec()`? – Alex Barker Nov 06 '19 at 20:26
  • I have tried this : ssh2_exec($connection2server_1, “telnet machine_2 portNumber). But how I send the command to the machine 2 ? If I send through ssh2_exec ($connection2server_1, $command) it will be send to server_1 and not to through the Telnet connection to marchine_2. – Jim Nov 06 '19 at 20:35
  • Oh, lulz. You can just redirect commands to telnet. Ex: `telnet machine_2 portNumber < – Alex Barker Nov 06 '19 at 20:46
  • Sorry for my basic question . I have just updated the question with the php code I am trying to implement , but unsuccessfully so far . – Jim Nov 06 '19 at 21:29
  • Assuming your telnet session doesn't require any auth, `$stream=@ssh2_exec($connection, 'telnet $machine_2 $port_telnet');` should become `$stream=@ssh2_exec($connection, "telnet $machine_2 $port_telnet <<< $command");` with double quotes. – Alex Barker Nov 06 '19 at 22:19
  • The Telnet session doesn't ask for username or password. I have tried your code, but the command was not executed. Is there a way for me to wait some time after the telnet session is established and then send the $command to this session? – Jim Nov 07 '19 at 15:03

0 Answers0