VPS 升級後因 Ubuntu Upstart 與 OpenVZ 相容性問題導致 SSH 服務無法自動啟動。利用 WordPress 後台仍可編輯主題檔案的特性,透過 PHP proc_open 執行 su 指令啟動 sshd 服務,恢復遠端存取。
proc_open 啟動 Python 程序,建立虛擬終端 (pty)。/bin/sh。su -c 'service ssh start' root 指令。su,提示輸入密碼。sleep 等待提示,再發送 root 密碼。header.php 臨時執行)<?php
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$process = proc_open("python -c 'import pty; pty.spawn(\"/bin/sh\")'", $descriptorspec, $pipes);
if (is_resource($process)) {
// 發送 su 指令啟動 ssh
fwrite($pipes[0], "su -c 'service ssh start' root\n");
fflush($pipes[0]);
sleep(3); // 等待密碼提示
// 發送 root 密碼 (請替換為實際密碼)
fwrite($pipes[0], "YOUR_ROOT_PASSWORD\n");
fflush($pipes[0]);
// 清理資源
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
?>
# Ubuntu 16.04+ 使用 systemd
systemctl enable ssh
systemctl start ssh
# 檢查服務狀態
systemctl status ssh