The popen
other than exec
and similar may interact similar to a Handler file I/O, probably with it will be able to catch the whole "exit", for example:
$response = '';
$handle = popen('apt-get remove ' . $package . ' 2>&1', 'r');
if ($handle) {
while (feof($handle) === false) {
$response .= fgets($handle); //Pega até encontrar uma quebra de linha
}
pclose($handle);
} else {
die('Erro ao executar o programa');
}
if (empty($response)) {
die('Resposta voltou vazia');
}
var_dump($response);
An example with ping
(which is something that returns the output one by one):
<?php
$response = '';
$handle = popen('ping 127.0.0.1', 'r');
if ($handle) {
while (feof($handle) === false) {
$response .= fgets($handle); //Pega até encontrar uma quebra de linha
}
pclose($handle);
} else {
die('Erro ao executar o programa');
}
if (empty($response)) {
die('Resposta voltou vazia');
}
var_dump($response);
If you want to show the result directly in the output (output) do the echo
within the while
, thus:
<?php
$response = '';
$handle = popen('ping 127.0.0.1', 'r');
if ($handle) {
while (feof($handle) === false) {
echo fgets($handle); //Pega até encontrar uma quebra de linha
}
pclose($handle);
} else {
die('Erro ao executar o programa');
}
The interesting thing in this way is that you can already pick up part of the answer and with a stripos
or preg_match
detect if the command is waiting type Y/N to confirm something and customize yourself for any situation you want, for example assuming you ran the apt-get remove
and the terminal was displayed something like:
Do you want to continue [Y/N]
The while
would catch in the fgets
which contains this command and at this time you could use a fwrite
, something like:
Note: changing the second parameter to a+
, which opens for reading and writing; places the file pointer at the end of Handle.
$handle = popen('apt-get remove ' . $package . ' 2>&1', 'a+');
if ($handle) {
while (feof($handle) === false) {
$response = trim(fgets($handle));
if (stripos($response, 'do you want to continue') !== false) {
fwrite($handle, "Y\n");// O \n é para enviar a quebra de linha que creio ser necessária para disparar
}
}
pclose($handle);
} else {
die('Erro ao executar o programa');
}
Have you tried something similar to this, https://stackoverflow.com/q/8217613/1452488?
– Woss
I managed with version 3.5: https://docs.python.org/3/library/subprocess.html
– AnthraxisBR
Edit¹. It doesn’t work that way either, just the same way with static response, like, in apt remove, I need to answer the 'y' in some cases, then I won’t be able to
– AnthraxisBR
Already tried to pass the option
-y
in command? It will automatically take over yes for any possible questioning of the command.– Woss
In Java, you have input, output and error output streams. And any other process-related file, I think. Don’t you have any of that in Python/PHP? (Of course, the command line option
-y
is the best chance)– Jefferson Quesado
@Andersoncarloswoss The problem was in capturing what had happened in the execution of the script, running the script via php or python, I managed to circumvent the command in an ssh connection.
– AnthraxisBR