i have tried many time by using flush() to make the script work synchronously, the script prints only data of the first command "gcloud compute ssh yellow" and "ls -la", I am looking to make the script prints the output on every executed fputs().
<?php
$descr = array( 0 => array('pipe','r',),1 => array('pipe','w',),2 => array('pipe','w',),);
$pipes = array();
$process = proc_open("gcloud compute ssh yellow", $descr, $pipes);
if (is_resource($process)) {
sleep(2);
$commands = ["ls -la", "cd /home", "ls", "sudo ifconfig", "ls -l"];
foreach ($commands as $command) {
fputs($pipes[0], $command . " \n");
while ($f = fgets($pipes[1])) {
echo $f;
}
}
fclose($pipes[0]);
fclose($pipes[1]);
while ($f = fgets($pipes[2])) {
echo "\n\n## ==>> ";
echo $f;
}
fclose($pipes[2]);
proc_close($process);
}
Thanks in advance
I believe the problem is the loop you have waiting for input.
fgetswill only return false if it encounters EOF. Otherwise it returns the line that it read; because the linefeed is included, it doesn't return anything that can be typecast to false. You can usestream_get_line()instead, which does not return the EOL character. Note this would still require your command to return an empty line after its output so it can evaluate to false and break the while loop.Another option would be to gather the output outside the loop, although this would require you to parse the output if you need to know what output came from what command.