我一直在尝试遵循在以下位置找到的示例代码:
http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/
我能够连接到我的交换机,并成功进行身份验证。但是当我尝试运行命令时,它会失败,并在这篇文章的标题中显示错误消息。
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("10.14.123.12", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "mypassword")) {
echo "fail: unable to authenticate\n";
} else {
// execute a command
if (!($stream = ssh2_exec($con, "show mac-address" ))) {
echo "fail: unable to execute command\n";
} else {
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
它在我尝试执行 show mac-address 命令的那一行上快死了。我已经确认这是 switch 命令的正确语法 - 手动尝试过......它返回数据就好了。有什么建议么?谢谢。