亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在PHP中運(yùn)行Linux命令并啟動(dòng)SSH服務(wù)的例子

 更新時(shí)間:2014年06月12日 09:13:23   投稿:junjie  
這篇文章主要介紹了在PHP中運(yùn)行Linux命令并啟動(dòng)SSH服務(wù)的例子,因?yàn)閂PS的SSH服務(wù)掛了,導(dǎo)致無法進(jìn)入服務(wù)器,所以想了這么一個(gè)辦法,需要的朋友可以參考下

升級 VPS 后,由于 Ubuntu 的 upstart 與 OpenVZ 的兼容問題,導(dǎo)致 sshd 服務(wù)不自動(dòng)啟動(dòng)了,在嘗試了 vePortal 的 console 與 file manager 及提交技術(shù)支持后都不能解決問題之后。

只能靠自己了,大概的思路是在 PHP 中進(jìn)行 su 命令以執(zhí)行 sshd 服務(wù),因?yàn)?WordPress 還活著,并且可以在后臺直接編輯主題相關(guān)的 PHP 腳本。只要把準(zhǔn)備好的代碼片斷插入到 header.php 中,并在瀏覽器中訪問一下主頁即可。

相關(guān)的代碼邏輯
1. 使用 PHP 的 proc_open 打開一個(gè)進(jìn)程,重定向 stdin, stdout, stderr, 這里會執(zhí)行一個(gè) python 程序。
2. 在這個(gè) python 程序中打開一個(gè) pty,并運(yùn)行一個(gè) sh。
3. 利用步驟 1 中重定向的 stdin pipe 向 python 程序發(fā)送 su 命令, python 會將來自 stdin 的命令數(shù)據(jù)寫到入 ptmx,而這時(shí) sh 的 stdin, stdout 及 stderr 是重定向到與 python 打開的 ptmx 配對的 pts 上的。也就是說 su 命令最終會轉(zhuǎn)給 sh 進(jìn)程處理。
4. sh 進(jìn)程自然的執(zhí)行了 su 命令,這時(shí) su 進(jìn)程的 stdin, stdout, stderr 也會被重定向到那個(gè) pts 上。
5. 在 sleep 一段時(shí)間后(主要是等 su 真的跑起來了),再寫入密碼,數(shù)據(jù)流過程與步驟 3、4 一致。

相關(guān)的代碼片斷:

復(fù)制代碼 代碼如下:

<?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)) {
    fwrite($pipes[0], "su -c 'service ssh start' root\n");
    fflush($pipes[0]);
    sleep(3);
    fwrite($pipes[0], "PASSWORD\n");
    fflush($pipes[0]);
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);
  }
?>

相關(guān)文章

最新評論