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

0

With the shell_exec command I get a response 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, would another command need to have an answer?) of executing a command when the answer is not a unique text like in the example:

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

Obs In the example it runs and uninstalls, but I can not tell if the command was executed or not.

    
asked by anonymous 12.02.2018 / 22:43

3 answers

1

popen other than exec and the like may interact similar to a file I / O handler, probably with it will be able to get the "output "all, 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 display the result directly in the output, make echo within while , like this:

<?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 is that you can already take part of the answer and with stripos or preg_match detect if the command is waiting for type Y / N to confirm something and customize yourself to situation you want, for example assuming you ran apt-get remove and the terminal displayed something like:

  

Do you want to continue [Y / N]

The while would lock in the fgets that contains this command and at this point you could use a fwrite , something like:

  

Note: changing the second parameter to a+ , which opens for reading and writing; puts the file pointer at the end of the 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');
}
    
14.02.2018 / 19:21
0

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

Here's an 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 server ssh (in this case myself), and execute the command, 'echo' printa the response that would come when executing on the terminal.

    
13.02.2018 / 03:32
0

Simple answer, go down.

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 has some problem in the command, then you should analyze the return code.

    
14.02.2018 / 18:13