[英]php ssh2_exec not executing 'su' command
我用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
在此先感謝任何幫助:)
喬
您應該嘗試最新的SVN版本的phpseclib - 一個純PHP SSH實現 - 而不是。 以下是你如何做到這一點:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('localhost', 22);
$ssh->login('username', 'password');
$ssh->read('[prompt]');
$ssh->write("su - user\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>
也許嘗試像bash -c su
或su root
和bash -c "su root"
?
您只需在執行“su”命令時附加超級用戶密碼即可。 您可以對代碼進行以下修改。
$cmd = "su";
$cmd = "echo '" . $sshPassword . "' | sudo -S " . $cmd;
if (!($stream = ssh2_exec($con, $cmd))) {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.