Run script and capture output?

Trying to find out how to run a Linux script in php, and to capture only part of the output for inclusion in a web page.

I’d like to execute the ‘wireless’ script, and echo only the rssi level (-31.x) from it’s output:

$ /bin/cat /proc/net/wireless

Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
 face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22

wlo1: 0000   70.  -31.  -256        0      0      0      0     25        0

Any clues or related links, anyone?

Note that the PHP executable user must have permissions to execute the script.
explode the result on the line breaks, then take the 3’th element and explode it on the space character and take the 3’th element.

1 Like

Thanks, m.

This look rational?..


$output = `/bin/cat /proc/net/wireless`;

$lines = explode("\n", $output);

$rssi_line = explode(" ", $lines[3]);

$rssi = $rssi_line[3];

echo "$rssi dBm";


I get your point about the permission requirements for the command in back-ticks.


Just for readability sake i’d pull the variable out of the string
echo $rssi." dBm";
but thats just me.

Looks good to me; the numbers may need adjusting as I’m not… entirely sure if that first line of the output is ‘actually there’ or it’s an artifact of the command line… so you may need to print_r($lines) to see what index to use.

Yep, all makes good sense.

That’s saved me re-learning the whole language from yeeears ago!

Muchly appreciated,

M. :slight_smile:

1 Like

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