【发布时间】:2011-08-09 15:10:57
【问题描述】:
我在 ssh2 for php 上玩得很开心。(!)
我正在通过 ssh-ing 到 localhost(运行 ubuntu)进行测试。我已经设法使用我的用户名(不是 root)连接和验证,并且一些命令(如 'ls' 返回一些信息,这是有希望的。肯定会到达某个地方。
接下来我想要做的是发出一个“su”命令,然后给出 root 密码。
我没有收到错误,并且返回了资源,但流中似乎没有数据。 (我有点期待“密码:”提示)。我无法直接使用 root 密码进行身份验证,因为 ssh 禁用了该密码。
你认为“su”有什么理由会返回一些文本吗?
我应该期待“密码:”提示返回吗?
这是我的代码:
function changeServerPassword( $ip, $port, $sshUser, $sshPassword, $rootPassword, $newRootPassword, $newSSHPassword = false) {
// login to server using $sshUser and $sshPassword
// su as root and enter $rootPassword
// if any of the above steps fail, return with appropriate error message
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in
// Do I have to make sure that port is a number?
if(!($con = ssh2_connect($ip, $port))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, $sshUser, $sshPassword)) {
echo "fail: unable to authenticate\n";
} else {
// alright, we're in!
echo "okay: logged in...<br />";
//
if (!($stream = ssh2_exec($con, "su"))) {
echo "fail: unable to execute command\n";
} else {
echo $stream."<br />";
// collect returning data from command
stream_set_blocking($stream, true);
echo "after stream_set_blocking<br />";
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
echo "data len: " . strlen($data) . "<br />";
echo $data."<br />";
fclose($stream);
}
}
}
}
借自http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ 尊重。
我得到的输出是:
okay: logged in...
Resource id #3
after stream_set_blocking
data len: 0
提前感谢您的帮助:)
乔
【问题讨论】:
-
提出更好的问题会得到更好的回答。
-
请提供更多信息的问题名称?
-
passwd使用 stderr 作为提示。不知道它是否相关,但您提到的其他工作命令都使用 stdout 作为输出。 -
现已编辑。以标题道歉。有点兴奋。