php ssh2通过跳转服务器连接到第二台服务器

时间:2019-04-09 09:09:13

标签: php ssh ssh2

===========              ===============            ===============
apache2 server    ====>     remote server A     =====>  remote server B
===========              ===============            ===============

我想通过远程服务器A从apache2服务器访问远程服务器B,因为远程服务器B仅接受来自服务器A(跳转主机)的连接。 我在Google和此网站上进行了搜索,但仍然没有运气。我尝试使用下面的脚本从apache2服务器连接服务器B,但没有结果输出显示。 我错过了什么?

<?php

$host = "192.168.xx.xxx";
$port = 22;
$username = "serverA";
$password = "serverApass";
$connection = NULL;

$passSvr = "serverBpass";
$SvrBUser = "serverB";
$SvrBHost = "192.168.xx.xxx";

try {
    $connection = ssh2_connect($host, $port);
    if(!$connection){
        throw new \Exception("Could not connect to $host on port $port");
    }
    $auth  = ssh2_auth_password($connection, $username, $password);
    if(!$auth){
        throw new \Exception("Could not authenticate with username $username and password ");
    }
    if($connection){

    $contents = ssh2_exec($connection, "sshpass -p '{$passSvr}' ssh $SvrBUser@$SvrBHost ls -al");
    stream_set_blocking($contents, true);
   $output = ssh2_fetch_stream($contents, SSH2_STREAM_STDIO);
   echo stream_get_contents($output);

        }

} catch (Exception $e) {
    echo "Error due to :".$e->getMessage();
}
?>

0 个答案:

没有答案