【问题标题】:PHP read console outputPHP读取控制台输出
【发布时间】:2009-08-28 09:13:54
【问题描述】:

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

另外,工作流程如下:

S - 网络服务器

C1 - S的内网中的computer1

C2 - S 的内网中的计算机 2

等等。

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

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

谢谢!

【问题讨论】:

  • 用户如何连接到服务器? SSH 还是什么?
  • 用户未通过 SSH 连接。用户只需访问 S 上的网页。服务器通过 SSH 或专有服务器软件连接到其 Intranet 中的计算机,但我对 SSH 选项的解决方案感兴趣。

标签: php linux console


【解决方案1】:

为此,您需要使用 php 函数 proc_open - 您可以指定管道数组(标准输入,如果您在控制台、标准输出和标准错误上,通常会附加到键盘上,两者通常都会打印到显示器上)。然后您可以控制给定程序的输入/输出流

举个例子:

$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!

【讨论】:

  • 哦,对于远程访问,S 将连接到 C(x),设置 SSH 密钥并将“$cmd 设置为:ssh hostname.of.c wget $url”
【解决方案2】:

您是否有一些现有的 PHP 代码正在处理?

【讨论】:

  • 目前不,这只是开始,但如果我无法读取控制台输出,恐怕我必须切换到其他东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 2019-12-24
  • 2023-03-21
  • 1970-01-01
  • 2013-10-18
  • 2016-06-05
  • 1970-01-01
相关资源
最近更新 更多