Running dmidecode from PHP script

Greetings,

Not sure if this is the right place for this but figured I'd start here. I am experimenting with PHP and HTML and I've wrote some code to return specific values when you point a browser at a server. The code is as follows in the /var/www/html/index.php file:

<?php
/* I used shell_exec a few times here for example's sake */
$hostname = shell_exec('hostname');
/* $hostname = `hostname`; */
$manufacturer = `dmidecode -s system-manufacturer | tail -1`;
$model = `dmidecode -s system-product-name | tail -1`;
$product = `dmidecode | grep SKU | cut -d: -f2`;
$serialno = `dmidecode -s system-serial-number | tail -1`;
$location = shell_exec('cat /etc/snmp/snmpd.conf | grep syslocation | grep -v Also | awk \'{for (i=2; i<NF; i++) printf \'\$i\' " "; print \'\$NF\'}\'');
/* $location = `cat /etc/snmp/snmpd.conf | grep syslocation | grep -v Also | awk '{print $2, $3, $4}'`; */
$contact = `echo "me@phonenumber"`;
$biosversion = `dmidecode -s bios-version | tail -1`;
$biosdate = `dmidecode -s bios-release-date | tail -1`;
$kernelarch = `uname -srvm`;
$processor = `dmidecode -s processor-version | tail -1`;
$opsys = `cat /etc/redhat-release`;
$ofedver = `ofed_info | head -1 | sed 's/://g'`;
$psid = shell_exec('ibv_devinfo | grep board_id | awk \'{print $2}\' | head -1');
$fwver = shell_exec('ibv_devinfo | grep fw_ver | awk \'{print $2}\'');
$glibcver = shell_exec('ldd --version | grep ldd | awk \'{print $4}\'');
$numprocessor = `cat /proc/cpuinfo | grep -c processor`;
$memcmd1 = `dmidecode | grep -E MB | grep -Ev 'BIOS|floppy|Range|Capacity' | awk '{ sum+=$2} END {print sum}'`;
$memcmd2 = `dmidecode | grep -E GB | grep -Ev 'BIOS|floppy|Range|Capacity' | awk '{ sum+=$2} END {print sum}'`;

if (strlen($memcmd1)>2) {
        $memtotal = $memcmd1 / 1000;
        $parts = preg_split('/[.]+/', $memtotal);
        $memtotal = $parts[0];
} elseif (strlen($memcmd2)>2) {
        $memtotal = $memcmd2;
}

$memfree = `expr \`cat /proc/meminfo | grep MemFree | awk '{print $2}'\``;
$memfree = $memfree / 1000000;
$uptime = `uptime | awk '{print $3, $4}' | tr -d ,`;
$iloipaddr = `ipmitool lan print | grep 'IP Address  ' | cut -d\: -f2 | sed 's/ //'`;
$sftwrlist = shell_exec('rpm -qa | sort');
?>

<p><b><u><big><?php echo $hostname;?></big></u></b><br/ >
<b>Manufacturer: </b><?php echo $manufacturer;?><br/ >
<b>Model: </b><?php echo $model;?><br/ >
<b>Serial Number: </b><?php echo $serialno;?><br/ >
<b>Product Number:</b><?php echo $product;?><br/ ><p/ >

<p><b>Location: </b><?php echo $location;?><br/ >
<b>Contact Information: </b><?php echo $contact;?><p/ >

<p><b>BIOS version and date: </b><?php echo "$biosversion $biosdate";?><br/ >
<b>Processor(s): </b><?php echo $processor;?><br/ >
<b>Kernel and architecture: </b><?php echo $kernelarch;?><br/ >
<b>Operating system: </b><?php echo $opsys;?><br/ >
<b>GLibC version: </b><?php echo $glibcver;?><br/ >
<b>OFED version: </b><?php echo $ofedver;?><br/ >
<b>IB riser card PSID: </b><?php echo $psid;?><br/ >
<b>IB riser card FW: </b><?php echo $fwver;?><p/ >

<p><b>Processor total: </b><?php echo $numprocessor;?><br/ >
<b>Total memory: </b><?php echo $memtotal;?>Gb<br/ >
<b>Free memory: </b><?php echo $memfree;?> Gb<p/ >

<p><b>UpTime: </b><?php echo $uptime;?><br/ ><p/ >

<p><a href="http://<?php echo $iloipaddr;?>">ILO link: You must be on the same private subnet for this link to work.</a><p/ >

<p><a href="sftwr.php">Installed Software</a><p/ >

The point: On RHEL7 this works exactly as I want and I had to change ipmitool and dmidecode to 4755 for it to return the valueson RHEL7 but on RHEL8 dmidecode doesn't work now but ipmitool does. So I'm wondering if anyone knows why RHEL8 is different in this regard from RHEL7? If giving a direct answer isn't suitable and you feel I should do the work myself, can I get a suggestion on how I might troubleshoot this? I've looked in the HTTP log files and didn't see anything jump out at me as to why dmidecode isn't executing or executing but failing for whatever reason. The HTTP config files don't look radically different from RHEL7 to RHEL8 but then again I'm not sure what to look for.
I should note that I created a small PHP script to run from the command line just to return a handful of values. I ran it as a non-privileged user on RHEL8 and it worked fine. So at least I know that despite the 4755 for dmidecode there isn't some other cryptic reason that dmidecode won't work for users other than root.
Thanks.

Can you run a shell command like

dmidecode -s system-product-name | tail -1

in a shell as a normal user?

Is selinux turned on?

getenforce
sestatus

In "enforcing" mode selinux controls if a certain process may access certain files.

Yes, since I changed the perms on dmidecode to 4755 any user can run it and Selinux is disabled. I'm thinking this is related to running it in the HTTP envrionment. Something changed from RHEL7 to RHEL8 to kill the ability to run dmidecode in this PHP script.

For kicks I created a small PHP script:

<?php
$manufacturer = `dmidecode -s system-manufacturer | tail -1`;
echo $manufacturer;
?>

And ran:

php -q "/var/www/html/test.php"

As a non-privileged user and it returns the expected value. But again, this will not work when using a browser.

Well I certainly feel like an idiot now. It just occurred to me that maybe I should try :

/usr/sbin/dmidecode

Instead of just:

dmidecode

And it now works. So clearly /usr/sbin was in the search path for RHEL7 and not RHEL8.

I had that in mind already :wink:
PATH is initialy inherited from the parent process, but there are mechanisms to change it.
In your case php inherits PATH from the web server., that inherited it from systemd. Maybe it is reset by a setting in an httpd.conf file or in a php conf file...

1 Like

Or just perhaps you've got two different versions of dmidecode on the system, an older version and a current version in different locations?

Well thanks for chiming in, very much appreciated.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.