简体   繁体   中英

Read command line output

Could any one help, How to read real time output from command terminal output in linux using php.

Below is code I tried but not working:

<?php
if (isset($_POST['pyinp'])){
$cmd = "ping 127.0.0.1";
$descriptorspec = 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("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="#" method="POST">
<input name = "pyinp" type="submit">
</form>
</body>
</html>

I am getting submit button and when i press submit nothing is coming.

I am new to html and php. I just code in online.

Below code is working:

<!DOCTYPE html>
<html>
<body>
<form action="#" method="POST">
<input name = "pyinp" type="submit">
</form>
<?php
if (isset($_POST['pyinp'])){
$cmd = "ping 127.0.0.1";
$descriptorspec = 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("pipe", "w")    // stderr is a pipe that the child will write to
);
echo "<pre>";
if( ($fp = popen("ls -l", "r")) ) {
    while( !feof($fp) ){
        echo fread($fp, 1024);
        flush(); // you have to flush buffer
    }
    fclose($fp);
}
}
?>
</body>
</html>

But it does not work if i replace with ping 127.0.0.1

Thanks

TL;DR: You have an infinite loop before the HTML document closes.


Let me rewrite your code, so that the ping output should go into the <body>...</body> container:

<!DOCTYPE html>
<html>
<body>
<form action="#" method="POST">
<input name = "pyinp" type="submit">
</form>
<?php
if (isset($_POST['pyinp'])){
$cmd = "ping 127.0.0.1";
$descriptorspec = 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("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";
}
?>

</body>
</html>

Now, let's do a thought experiment. First time you GET the page, you see a form with a button. All is well. You click the button. The form gets POST ed back to itself. Then what happens?

First, the static HTML is output:

<!DOCTYPE html>
<html>
<body>
<form action="#" method="POST">
<input name = "pyinp" type="submit">
</form>

then the PHP engine activates:

<?php

Then PHP sees the button was pressed, and does a bunch of stuff to setup pipes (all of this is correct, BTW, you can check on the command line):

$cmd = "ping 127.0.0.1";
$descriptorspec = 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("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";

Then it checks that the connection is made (you really should add an else here):

if (is_resource($process)) {

Now what?

while ($s = fgets($pipes[1])) {
    print $s;
    flush();
}

That's right: a while loop that never ends! This spins and spins forever. Meanwhile, none of this gets executed:

?>

</body>
</html>

So the browser never sees the body close . So what, you ask? Well, the browser is under no obligation to display content until it has closed all tags.

"But, but, it does work sometimes!" you scream. Sure, sometimes it works. But this time it looks like the loading has just stalled , not just bad HTML. The browser doesn't know that you've got an infinite loop that will never end, so it's just waiting for you to close up your output.

In general :

Web authors beware - unless you want to appear as an example in a Webkit error tolerance code - write well formed HTML.

If you want to see this work, then don't do an infinite loop:

$i = 0;
while ($s = fgets($pipes[1])) {
    print $s;
    flush();
    if (4 < $i++) { break; }
}

Or have Javascript load, calling back to a script that does the work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM