Hello all,
I am writing a ping function to have PHP ping a specific server and check how many packets returned to the ping request. The ping itself works, I get the output on screen and it all looks OK.
Here's the source so far:
function ping($server) {
$ping = system('ping -c 2 -t 5 ' . $server . '');
$reply = preg_match('/([0-2]) packets received/', $ping);
$packets = preg_split('[0-2]', $reply);
if($packets>0) {
$status = "<font color=\"green\">Online</font>";
} elseif($packets==0) {
$status = "<font color=\"red\">Offline</font>";
} else {
$status = "<font color=\"black\">Unknown</font>";
}
return $status;
}
// Test ping: Google.com
ping('www.google.com');
This script does ping, however it does not give the Online/Offline/Unknown response. Why is this ? I think my regex might not be OK, never been very good at them.
Also, is there a way to supress the raw server reply on the ping command and have it say "Pinging..." instead ? And replace that with Online/Offline/Unknown when the command executed ?