我有一個簡單的腳本我試圖運行:如何在php中將exec()與ssh'hostname'命令結合使用?
<?php
print exec('whoami');
$output2 = exec('ssh someotherhost ls -l /path/to/dir',$output);
print_r($output);
print_r($output2);
print $output2;
?>
這個腳本的目標是到另一臺聯網的服務器上運行的命令。如果我從命令行運行以上ssh
命令(用真實數據替換虛擬數據): ssh someotherhost ls -l /path/to/dir
它輸出正確的ls行。但是,當我使用相同的命令從相同的目錄運行上述腳本時,它不會輸出到三條底部打印行中的任何一條。但是,頂部的與whoami
按預期打印出來。所以我的問題是,爲什麼第一個命令工作,而不是第二個?
請注意,兩臺聯網服務器位於內部網絡上,並且使用ssh網絡密鑰對進行設置。該命令的作品,而不是從內部的PHP。
感謝您的幫助。
你在用什麼用戶?當你從命令行以'sudo'作爲該用戶時,它是否工作? – 2011-12-29 17:51:23
僅供診斷:請將「#!/ bin/sh \ n ssh someotherhost ls -l/path/to/dir」放入shell腳本中,並將其作爲「print exec(' whoami');「,chmod 700它。然後從命令行通過exec()從php中嘗試它 – 2011-12-29 18:00:09