Reputation: 49
Good day,
I am working on a system which checks whether a certain IP address is up or down. I have a MySQL DB with a table containing a list of IP addresses.I have created the following function to ping the IP address:
function pingAddress($ip) {
$result = exec('ping $ip -i 15 ', $outcome, $status);
if (0 == $status) {
echo 'Server UP ';
} else {
echo 'Server down ';
}
}
and in my while loop I have the following:
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
?>
<tr>
<td><?php pingAddress($row['client_ip_address']); ?></td>
</tr>
<?php
} //end while
?>
However instead of getting 0 (because servers are up) I always get 2.
I have replaced the line $result = exec('ping $ip -i 15 ', $outcome, $status);
with $pingresult = system("ping -n 3 $ip 2>&1", $outcome, $status);
OR $pingresult = shell_exec('/bin/ping -qc 1 '.$ip.' | awk -F/ \'/^rtt/\'', $result); OR ('ping $ip -i 15 ', $outcome, $status);
Still the result didn't change. Either I got an empty array (when I do var_dump($status)
or var_dump($outcome)
).
I am using Centos7 with apache installed on it.
I am a missing something because I have tried virtually the majority of solutions suggested here based on previous similar questions but it didn't work.
Is there a much better way code such a system?
Thanks for your assistance.
Upvotes: 0
Views: 1085
Reputation: 47264
You likely need to modify the syntax of exec
:
$result = exec('ping -i 15 -c 3 ' . $ip, $outcome, $status);
Also, you might include -c
or whatever your ping count
option is... otherwise it will run forever.
Upvotes: 2