我必须通过 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 做到这一点?