PHP 读取控制台输出

标签 php linux console

好的,事情就是这样。我需要读取输出(您通常在 Linux 控制台中看到的输出)。我最大的问题是我不需要读取线性执行的输出,而是需要读取类似 wget http://ubuntu.com/jaunty.iso 的内容并显示其 ETA。

此外,工作流程如下:

S - 网络服务器

C1 - S内网中的computer1

C2 - S内网计算机2

等等。

用户连接到S,它连接到Cx,然后启动wgettop 或其他控制台日志记录命令(根据用户的请求)。当wget下载指定目标时,用户可以从Cx看到“控制台日志”。

这合理吗?可以不使用服务器/客户端软件来完成吗?

谢谢!

最佳答案

您需要使用 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/

相关文章:

javascript - Microsoft Edge background.js 不会打印到控制台

php - 数据未显示在 Codeigniter View 中

linux kernel crypto API,在AES-GCM算法中如何设置aad的长度为零位?

c++ - 为什么 SO_RCVTIMEO 从监听套接字继承到接受套接字?

linux - 什么实际打印 "Segmentation fault"?

javascript - 将自定义功能添加到 chrome 的控制台中

php - Google Drive 存储图像 - 好方法还是坏方法

php - 在 codeigniter 中将数据显示到文本框中

php - SQL - 如何获取创建的同一日期的最新数据

javascript - Chrome DevTools 控制台文本突出显示颜色为黑色