首页 > 解决方案 > PHP - SSH 连接中的 Telnet 连接/命令

问题描述

我有我的网络拓扑:machine_1、server_1 和 machine_2。

我正在开发一个将托管在 machine_1 中的应用程序。该应用程序的功能之一是通过 ssh 连接(我使用过 ssh2_exec)连接到 server_1,然后 server_1 能够到达 machine_2。

我想要做的是在这个 server_1 内发送一个 telnet 命令到 machine_2。由于我已经通过 ssh2_exec 命令连接到 server_1,有没有办法建立这个 telnet 连接,然后在 PHP 中发送 telnet 命令?

*obs:我只能通过从 server_1 建立的 telnet 连接将命令发送到 machine_2。

<?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);
        }
    }         
}

标签: phpsshtelnet

解决方案


推荐阅读