3

我必须通过 ssh 连接到服务器,但要访问它,我需要先连接到另一个 ssh 服务器。我使用标准密码访问它们。

所以我的步骤是:

ssh root@serverdomain1.com

然后在 serverdomain1 中连接时,我在终端中执行以下操作:

ssh myuseraccount@serverdomain2.com

在 php 中,我尝试使用 ssh2_exec('ssh serverdomain2.com'); 但没有结果。然后我也尝试了 ss2_tunnel($connection, ...)。但没有任何效果。

这不起作用:

$ssh = ssh2_connect('serverdomain1.com', 22);
if (ssh2_auth_password($ssh, $user,$pass)) {
    $stream = ssh_exec($ssh, "ssh serverdomain2.com");

    stream_set_blocking($stream, true);
    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
    echo stream_get_contents($stream_out);    // <== doesn't work!!!
}

这也不起作用:

$ssh = ssh2_connect('serverdomain1.com', 22);
if (ssh2_auth_password($ssh, $user,$pass)) {
    $tunnel = ssh2_tunnel($ssh, 'serverdomain2.com', 22);

    if (!$tunnel) { 
        echo('no tunnel<br/>');
    }
    else {
        fwrite($tunnel, "echo 1\n");
        while (!feof($tunnel)) {
            echo fgets($tunnel, 128);
        }
    }    
}

隧道的回显结果:“SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.4 协议不匹配。”

如何使用 PHP 中的 SSH2 做到这一点?

4

2 回答 2

1

我最近发布了一个项目,它允许 PHP 获取并与真正的 Bash shell 交互,如果需要,可以通过 SSH。在这里获取:https ://github.com/merlinthemagic/MTS

该项目允许您使用 ssh 在服务器之间不断跳转。

下载后,您只需使用以下代码:

//first you get a shell on the first server:
 $shellObj = \MTS\Factories::getDevices()->getRemoteHost('ip_address1')->setConnectionDetail('username1', 'password1')->getShell();

//then build on that first shell, the following way.
\MTS\Factories::getDevices()->getRemoteHost('ip_address2')->setConnectionDetail('username2', 'password2')->getShell($shellObj);


//any command executed on the shell will run only on the second host you connected to.
$return1  = $shellObj->exeCmd("hostname");
echo $return1;//hostname of the second host you connected to

//

于 2016-05-23T13:03:19.873 回答
0
  1. 确保您的服务器上安装了 RSSH、PECL、SSH2 库
  2. 您可以使用 phpinfo 进行检查

这是我使用 ssh2 访问服务器的工作代码。希望它会有所帮助!

<?php
        $host = 'SERVER_HOST_ADDR';
        $port = SERVER_PORT;
        $username = 'SERVER_USERNAME';
        $password = 'SERVER_PASSWORD';
        $remoteDir = './home/'; //DIR_PATH
        $localDir = '/var/www/html/';        //LOCAL_DIR_PATH

        // Make our connection
        $connection = ssh2_connect($host);

        // Authenticate
        if (!ssh2_auth_password($connection, $username, $password)) {
            throw new Exception('Unable to connect.');
        }

        // Create our SFTP resource
        if (!$sftp = ssh2_sftp($connection)) {
            throw new Exception('Unable to create SFTP connection.');
        }

        /**
          * Now that we have our SFTP resource, we can open a directory resource
          * to get us a list of files. Here we will use the $sftp resource in
          * our address string as I previously mentioned since our ssh2:// 
          * protocol allows it.
          */
        $files = array();
        $dirHandle = opendir("ssh2.sftp://$sftp/$remoteDir");

        // Properly scan through the directory for files, ignoring directory indexes (. & ..)
        while (false !== ($file = readdir($dirHandle))) {
            if ($file != '.' && $file != '..') {
                $files[] = $file;
            }
        }
       echo "<pre>";print_r($files);
    ?>
于 2016-04-14T06:18:28.230 回答