Reputation: 31
I have a simple PHP script setup to check the status of my servers. It uses a standard ping command, run via exec().
Pinging with the same command via console works fine and times out correctly.
What's the cause, and how would this be fixed?
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$exec_string = 'ping -n 1 -i 255 -w 2 ' . $host;
} else {
$exec_string = 'ping -n -c 1 -t 255 -w 2 ' . $host;
}
exec($exec_string, $output, $return);
Upvotes: 2
Views: 1011
Reputation: 31
I'm unsure why, but switching from suPHP to fastCGI (both with suEXEC enabled) seemed to resolve the issue and the ping properly times out as expected.
If anyone has an explanation for this, I would love to know, in either comment or answer format.
Upvotes: 1
Reputation: 16074
I would avoid pinging directly using an exec()
. I use this script, found here, you can also setup port and timeout:
function ping($host, $port = 80, $timeout = 6) {
$fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fsock) {
return false;
} else {
return true;
}
}
$host = 'www.example.com';
if(ping($host)) {
echo "HOST UP";
} else {
echo "HOST DOWN";
}
Upvotes: 2