PHP – Send Shell Commands with shell_exec()

The shell_exec() function allows you to send commands to the Command Line using PHP. You can use this for everything from pinging websites, to calling installed apps such as FFMPEG.

shell_exec() also returns the results from the command such as Ping or Traceroute results.

WARNING:

  • EVERYONE needs permission to run whatever commands you are using with shell_exec().
  • The Return value from shell_exec() will only be provided once the return is complete so if you submit shell_exec(“ping cnn.com”); it will never return because there is no limiter.
  • Use the PRE tags around the shell_exec() return so that it is printed in the web browser properly.

shell_exec()

shellExecTest.php

<form action="shellExecTest.php" method="post">
command? <input type="text" name="command">
<input type="submit">
</form>

<?php

$command = $_POST['command'];
echo "<pre>";
echo shell_exec($command);
echo "</pre>";

?>

shellExecPing.php

<form action="shellExecPing.php" method="post">
Website? <input type="text" name="website">
<input type="submit">
</form>

<?php

$website = $_POST['website'];
$command = "ping -c 1 ".$website;

echo "Website: ".$command;
echo "<pre>";
echo shell_exec($command);
echo "</pre>";

echo "<h4>If/Else Statement</h4>";

if (shell_exec($command)== TRUE) {
    echo "Website UP";
} else {
    echo "website DOWN";
}

?>

picCompressApp.php

<h1>Pic Resize App</h1>
<form action="picCompressApp.php" method="post" enctype="multipart/form-data">
        <input type="file" name="Upload">
        <input type="submit">
    </form>

<?php

$dir = "./pics/";
$filename = $dir.basename($_FILES['Upload']['name']);
$filenameArray = pathinfo($filename);
$ext = array("jpeg","jpg","gif");
$isGood = 0;
$outputPic50 = $dir."50".basename($_FILES['Upload']['name']);
$outputPic100 = $dir."100".basename($_FILES['Upload']['name']);
$outputPic250 = $dir."250".basename($_FILES['Upload']['name']);
$outputPic500 = $dir."500".basename($_FILES['Upload']['name']);

if (file_exists($filename)) {
    echo "The file already exists<br>";
    $isGood = 1;
}

if ($_FILES['Upload']['size'] > 500000) {
    echo "File is over 500kB in size<br>";
    $isGood = 1;
}

if (!in_array($filenameArray['extension'],$ext)) {
    echo "File Type is not Allowed (Upload jpeg, jpg,gif)<br>";
    $isGood = 1;
}

if ($isGood != 1) {
    if (move_uploaded_file($_FILES['Upload']['tmp_name'], $filename)){
        echo "<p>File was uploaded --> ".$_FILES['Upload']['name'];
    } else {
     echo "Upload failed".$_FILES['Upload']['name'];
    }
}

shell_exec("ffmpeg -i '$filename' -vf scale=50:-1 '$outputPic50'");

shell_exec("ffmpeg -i '$filename' -vf scale=100:-1 '$outputPic100'");

shell_exec("ffmpeg -i '$filename' -vf scale=250:-1 '$outputPic250'");

shell_exec("ffmpeg -i '$filename' -vf scale=500:-1 '$outputPic500'");

echo "<br><img src=".$outputPic50."><br><br>";
echo "<img src=".$outputPic100."><br><br>";
echo "<img src=".$outputPic250."><br><br>";
echo "<img src=".$outputPic500."><br><br>";
echo "<img src=".$filename.">";
?>

Be the first to comment

Leave a Reply