【发布时间】:2014-12-18 22:16:50
【问题描述】:
下面的代码可以正常工作,它完美地返回了我连接到 SSH 服务器的用户名。是否可以创建某种交互式 shell,我可以自己发送命令。就像 Mac 上的黑色终端窗口一样。我看过一些关于 ssh2_shell 函数的文档,您可以在其中指定终端窗口的宽度和高度,但无法使其正常工作。
$server['ip'] = "127.0.0.1";
$server['sshport'] = 22;
$server['user'] = "myUsername";
$server['pw'] = "myPassword!";
$command = "whoami";
if($ssh = ssh2_connect($server['ip'], $server['sshport'])) {
if(ssh2_auth_password($ssh, $server['user'], $server['pw'])) {
$stream = ssh2_exec($ssh, $command);
stream_set_blocking($stream, true);
$data = "";
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
fclose($stream);
print $data;
} else {
echo "User or password is incorrect!";
}
} else {
echo "Could not connect to the ssh server!";
}
【问题讨论】: