Bogdan Constantinescu
Bogdan Constantinescu

Reputation: 5356

PHP read console output

Ok, so here's the thing. I need to read the output (the one that you usually see in a linux console). My biggest problem is that I don't need to read the output of a linear execution, but something rather like wget http://ubuntu.com/jaunty.iso and show its ETA.

Also, the work-flow is the following:

S - webserver

C1 - computer1 in S's intranet

C2 - computer 2 in S's intranet

and so on.

User connects to S which connects to Cx then starts a wget, top or other console logging command (at user's request). User can see the "console log" from Cx while wget downloads the specified target.

Is this plausible? Can it be done without using a server/client software?

Thanks!

Upvotes: 2

Views: 3821

Answers (2)

Josh
Josh

Reputation: 11070

You'll want to use the php function proc_open for this -- you can specify an array of pipes (stdin, which would normally be attached to the keyboard if you were on the console, std out, and stderr, both normally would be printed to the display). You can then control the input/output folw of the given program

So as an example:

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

Upvotes: 3

bucabay
bucabay

Reputation: 5295

Do you have some existing PHP code you're working on?

Upvotes: 0

Related Questions