1
我想通過PHP使用SSH連接來運行Bash腳本。如何使用PHP分別運行2個SSH命令?
我寫了一個腳本來進行備份和恢復的MySQL數據庫和我還在測試,以實現這一目標。我在嘗試 運行兩個不同的簡單命令時遇到了問題。我的代碼是:
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("server.hosting.com", 22))){
echo "fail: unable to establish connection\n";
} else {
if(!ssh2_auth_password($con, "username", "password")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
if (!($stream = ssh2_exec($con, "cd directory "))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
if (!($stream = ssh2_exec($con, "mkdir directiry2"))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
?>
它仍然創建另一個目錄,但不在「目錄」裏面! 請幫忙!!!
爲什麼不'的mkdir -p目錄/ directiry2'?你不需要cd進入一個目錄來製作其中的內容。 –
感謝那個馬克B,現在我該如何運行這樣的東西: 'mysqldump -u ... -p ... mydb t1 t2 t3> mydb_tables.sql' –
[無恥的酒吧]你可能想要一個看https://github.com/tivie/command 它可以讓您鏈shell命令和一個面向對象的方式設定CWD – Tivie