好的,事情就是这样。我需要读取输出(您通常在 Linux 控制台中看到的输出)。我最大的问题是我不需要读取线性执行的输出,而是需要读取类似 wget http://ubuntu.com/jaunty.iso
的内容并显示其 ETA。
此外,工作流程如下:
S
- 网络服务器
C
1 - S内网中的computer1
C
2 - S内网计算机2
等等。
用户连接到S
,它连接到C
x,然后启动wget
,top
或其他控制台日志记录命令(根据用户的请求)。当wget
下载指定目标时,用户可以从C
x看到“控制台日志”。
这合理吗?可以不使用服务器/客户端软件来完成吗?
谢谢!
最佳答案
您需要使用 php 函数 proc_open为此,您可以指定一个管道数组(stdin,如果您在控制台上,则通常会连接到键盘,std out 和 stderr,通常都会打印到显示器上)。然后您可以控制给定程序的输入/输出
举个例子:
$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!
关于PHP 读取控制台输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1345880/