How to get response from 'apt remove' via shell_exec with PHP or Python?

Asked

Viewed 259 times

0

With the shell_exec command I can get an answer if the result of the executed command is a single text:

shel_exec('dpkg -l > list-softwares-dpkg.txt');

How to have the answer (in PHP or Python, you would need another command to have an answer ?) to execute a command when the answer is not a single text as in the example:

shel_exec('apt remove pacote_exemplo > removed-package.txt');

Obs In the example it executes and uninstalls, however I cannot know if the command was executed or not.

  • Have you tried something similar to this, https://stackoverflow.com/q/8217613/1452488?

  • I managed with version 3.5: https://docs.python.org/3/library/subprocess.html

  • 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

  • Already tried to pass the option -y in command? It will automatically take over yes for any possible questioning of the command.

  • 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)

  • @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.

Show 1 more comment

3 answers

1


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');
}

0

The solution found was in PHP, using the class SSH2 package phplibsec to execute the command.

Following example:

    $input = 'apt-get remove ' . $package . ' -y';
    $ssh = new \phpseclib\Net\SSH2('localhost');
    if (!$ssh->login('user', 'pass')) {
        exit('Erro ao logar');
    }
    echo json_encode($ssh->exec($input));

In the example, connect to the ssh of the server (in case myself), and run the command, 'echo' prints the response that would come when running on the terminal.

0

Simple answer, go below.

shel_exec('apt remove pacote_exemplo > removed-package.txt'; echo $?);

the variable $? returns the return code of the last executed command. If the return code is different from 0 there is some problem in the command, then you should analyze the return code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.