2009-08-28 189 views
2

好的,這是事情。我需要讀取輸出(通常在Linux控制檯中看到的輸出)。我最大的問題是我不需要讀取線性執行的輸出,而是像wget http://ubuntu.com/jaunty.iso,並顯示它的ETA。PHP讀取控制檯輸出

另外,工作流程如下:

S - 網絡服務器

C - 電腦1 S中的內聯網

C - 計算機2 S中的內聯網

等等。

用戶連接到S它連接到C X然後開始一個wgettop或其他控制檯記錄命令(在用戶的請求)。用戶可以從C x中看到「控制檯日誌」,而wget下載指定的目標。

這是否合理?可以在不使用服務器/客戶端軟件的情況下完成嗎?

謝謝!

+0

怎樣的用戶連接到服務器? SSH還是什麼? – n1313 2009-08-28 09:53:25

+0

用戶不通過SSH進行連接。用戶只需進入S上的網頁。服務器通過SSH或專有服務器軟件連接到其Intranet中的計算機,但我對SSH選項的解決方案感興趣。 – 2009-08-28 11:04:33

回答

3

你會想爲此使用php函數proc_open - 你可以指定一個管道數組(標準輸入,如果你在控制檯,標準輸出和標準輸入,通常會連接到鍵盤上)通常會打印到顯示屏上)。然後,您可以控制給定的程序

這樣的輸入/輸出folw爲例:

$pipes = array(); 
$pipe_descriptions = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to 
); 

$cmd = "wget $url"; 

$proc_handle = proc_open($cmd, $pipe_descriptions, $pipes); 

if(is_resource($proc_handle)) 
{ 
    // Your process is running. You may now read it's output with fread($pipes[1]) and fread($pipes[2]) 
    // Send input to your program with fwrite($pipes[0]) 
    // remember to fclose() all pipes and fclose() the process when you're done! 
+0

哦,對於遠程訪問,S將連接到C(x),設置SSH密鑰並將「$ cmd設置爲:ssh hostname.of.c wget $ url」 – Josh 2009-08-31 13:00:35

0

你有一些你正在使用的PHP代碼嗎?

+0

目前沒有,它只是開始,但是如果我不能讀取控制檯輸出,恐怕我不得不切換到別的東西。 – 2009-08-28 11:06:10