GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


php:execute_a_shell_command_and_capture_the_output_into_an_array
function my_exec($cmd, $input='') {
    $cmd=escapeshellcmd($cmd); 
    $proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes); 
    fwrite($pipes[0], $input);fclose($pipes[0]); 
    $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]); 
    $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]); 
    $rtn=proc_close($proc); 
    return array('stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$rtn ); 
}

$cmd should be the command (e.g. trace route -l 1.2.3.4) and $input can be used to inject std_in to the command if needed.

/data/webs/external/dokuwiki/data/pages/php/execute_a_shell_command_and_capture_the_output_into_an_array.txt · Last modified: 2019/05/20 10:15 by genadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki