在PHP命令我嘗試問題:發出多個慶典從遠程計算機
sudo apt-get update
和
sudo apt-get install openjdk-7-jre joe wget
從PHP遠程機器上
,
我一直環顧四周,嘗試了兩種方法:
第一個:
session_start();
$command = "ssh -o 'StrictHostKeyChecking no' -i /var/www/.ssh/my-keypair555.pem [email protected]{$_SESSION['host']} \"sudo apt-get update\"";
echo $command."<br/>";
echo exec($command, $output);
print_r($output);
$command = "ssh -o 'StrictHostKeyChecking no' -i /var/www/.ssh/my-keypair555.pem [email protected]{$_SESSION['host']} \"sudo apt-get install -y openjdk-7-jre joe wget\"";
echo $command."<br/>";
echo exec($command, $output);
print_r($output);
此代碼似乎並沒有得到在所有執行, (該命令獲取打印,但在輸出數組似乎空。)
其次: 從http://php.net/manual/en/function.ssh2-exec.php
session_start();
$connection = ssh2_connect($_SESSION['host'], 22, array('hostkey'=>'ssh-rsa'));
if (ssh2_auth_pubkey_file(
$connection,
'ubuntu',
'/var/www/.ssh/id_rsa.pub',
'/var/www/.ssh/id_rsa')
) {
$shell = ssh2_shell($connection,"bash");
$cmd = "echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'";
$output = user_exec($shell,$cmd);
fclose($shell);
echo $output."<br/>";
} else {
die('Public Key Authentication Failed');
}
function user_exec($shell,$cmd) {
fwrite($shell,$cmd . "\n");
$output = "";
$start = false;
$start_time = time();
$max_time = 900; //time in seconds
while(((time()-$start_time) < $max_time)) {
$line = fgets($shell);
//echo $line;
if(!strstr($line,$cmd)) {
if(preg_match('/\[start\]/',$line)) {
$start = true;
echo "start";
}elseif(preg_match('/\[end\]/',$line)) {
return $output;
}elseif($start){
$output[] = $line;
echo $line."<br/>";
}
}
}
}
改編版本
如果我使用此代碼的命令:
echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'
確實顯示如果我登錄到機器並出具
history
但似乎並沒有被執行,因爲如果我發出
java
,我感到我需要安裝它的消息。
如果我發出
!1
使用apt-get更新和apt-get安裝都得到正確執行。
如果我運行在瀏覽器中的PHP文件我得到以下的輸出:
歡迎的Ubuntu 14.04.1 LTS(GNU/Linux的3.13.0-36泛型x86_64的)*文檔:爲https://help.ubuntu.com/系統信息Sun Feb 8 17:20:45 UTC 2015系統負載:0.69進程:104 /的使用情況:7.74GB的9.8%登錄用戶:0內存使用情況:1%eth0的IP地址:10.74.190.178交換使用情況:0%此數據並在以下位置管理此係統:https://landscape.canonical.com/通過Ubuntu Advantage Cloud獲得雲支持來賓:http://www.ubuntu.com/business/services/cloud可以更新0個軟件包。 0更新是安全更新。上次登錄:Sun Feb 8 17:20:50 2015 from ec2-54-77-56-210.eu-west-1.compute.amazonaws.com echo'[start]'; sudo apt-get update; sudo apt- get install -y openjdk-7-jre joe wget; echo'[end]'ubuntu @ ip-10-74-190-178:〜$ echo'[start]'; sudo apt-get update; sudo apt-get in
- 很多空行和現在,然後像 ALAL 或 升-l雙消息 -
- >請注意,命令在截斷 '安裝'
這將解釋爲什麼通信並沒有執行。
誰能給我一隻手就這一個,我一直在尋找近兩週,似乎沒有進步很多...
感謝,
桑德