Executing the "uptime" unix shell command via PHP

Category: Software and Systems Engineering
Author: Sean O'Donnell
Thu, Feb. 20th, 2003 @ 2:20:22 (MST)

This example demonstrates using the 'exec' function in PHP, to execute simple shell commands, such as 'uptime'.

There are many other ways to execute programs via PHP, and many other programs I could have used as an example, but this example should be pretty straight-forward.

<?php
$cmd 
'uptime';
$results exec(escapeshellcmd($cmd));
print 
$results;
?>

Here I use the 'escapeshellcmd' command to ensure that no arbitrary commands are being executed by the 'exec' function, which is what is actually executing the command.

This is most significantly important if you're executing a command that may contain user-input data via HTTP Request, or something.

Example (Results):

12:24:12 up 248 days, 12:09,  29 users,  load average: 0.00, 0.00, 0.00

Copyleft (<) 1998-2019 www.seanodonnell.com