Using PHP to Display Version Info – Part II

Posted by Nessa | Posted in ,,, | Posted on October 17, 2007

3

Earlier I showed some examples of commands you can use to pull software versions into a simple PHP script. In the next example I’ll show how to do the distro and kernel versions. I’m sure that there’s a much simpler way to do this (i.e. a simple cat command on the /proc/version file), but I figured I’d use a more advanced example in the form of a function. I did the distro as a variable to show how easy this is:


<?php
function kernelinfo () {
$buf = exec( 'cat /proc/version');
if ( $buf == "ERROR" ) {
$result = "N.A.";
} else {
if (preg_match('/version (.*?) /', $buf, $ar_buf)) {
$result = $ar_buf[1];


if (preg_match('/SMP/', $buf)) {
$result .= ' (SMP)';
}
}
}
return $result;
}


$distro = exec('cat /etc/redhat-release');


$kernel = kernelinfo();


echo $distro;


echo "<html><br></html>";


echo $kernel;

?>

This will show a result of the following, obviously various depending on your platform:

CentOS release 4.5 (Final)
2.6.9-023stab033.9-enterprise (SMP)

Keep in mind that this script (as the last) requires the ability to pass shell commands through PHP. So, you would need to be able to the exec function, or replace it with system, passthru, escapeshellcmd, or shell_exec if those are allowed in your php.ini configuration. Also, if you run PHP as an Apache module, this will not work if you have open_basedir protection enabled.

Be Sociable, Share!

Comments (3)

[…] I’ll eventually get the whole thing up here, but I’ve been working on a simple server info script to help me and the other members of the system admin team keep up with the gazillion servers we have and all their different configurations. One of the reasons it’s taking so long (aside from my recent alcohol binges) is that it has to be portable to every server without the need for specific modifications, regardless of their setup. This eventually calls for using a simple if statement and empty() function to decide what info to output. Really, it’s so easy that I don’t even know why I’m posting it, but it kinda supplements this and this. […]

Hey,

Couldn’t get it to work under Fedora Core 9, the following error is outputted.

Parse error: syntax error, unexpected T_STRING in /var/www/html/script.php on line 11.

Line 11 is: $result .= ‘ (SMP)’;

I wrote that example based on CentOS – you probably need to edit the preg_match paramters, esp. if your kernel is non-SMP.

Post a comment