Reading Console in PHP

Okay, so here's the deal. I need to read the result (the one you usually see in the linux console). My biggest problem is that I don't need to read the linear execution result, but something like wget http://ubuntu.com/jaunty.iso

and show it to ETA.

In addition, the workflow is as follows:

S

- web server

C

1 - computer1 on S intranet

C

2 - computer 2 on the S intranet

etc.

User connects to S

, who connects toC

x, then runs a command wget

, top

or another console (at the user's request). User can see the "console log" fromC

xand wget

loads the specified target.

Is this believable? Is it possible to do this without using server / client software?

Thank!

+2


source to share


2 answers


You want to use php's proc_open function to do this - you can specify an array of pipes (stdin, which will usually be attached to the keyboard if you were at the console, std out and stderr, both are usually printed to the display). Then you can control the input / output data of this 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 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!

      

+3


source


Do you have any existing PHP code that you are working on?



0


source







All Articles