Shell Command App

This project allows you to send commands directly to the Linux OS and print out the results on a web page. This can be useful for creating web based administrative controls for a server, or for testing results from tools such as PING to create an up/down web dashboard.

WARNING:

The function you will use shell_exec() only returns a value once the command has finished running. For tools like PING they do not automatically stop in Linux the way they do on Windows. For PING make sure to set a specific PING count such as ping -c 1 cnn.com which will ping cnn.com once.

Code Explanation:

Lines 3-6 – Create an HTML form with a single text input box. This is a single page script and so the POST value will be sent back to the command.php script.

Line 10 – Takes the value posted from the form and assigns it to the $command variable

Line 12 – Runs the function shell_exec() which executes a shell command and returns the response from the OS. shell_exec($command) returns the results from the command and assigns the value to $ result.

Line 14 – echo the value for $result inside <pre></pre> tags. The results from shell_exec() are standard text, without the <pre> tags in HTML the response will simply be printed out as a single line.

command.php

<h1>Command App</h1> <form action="command.php" method="post"> <input type="text" name="command"> <input type="submit"> </form> <?php $command = $_POST['command']; $result = shell_exec($command); echo "<pre>$result</pre>"; ?>
Code language: HTML, XML (xml)