[Note: Since writing this post, I've created the Ping class for PHP, which incorporates three different ping/latency/uptime methods for PHP, and is a lot more robust than the script I have posted below.]
I recently needed to display some ping/server statistics on a website using PHP. The simplest way to do something like this is to use the built-in linux utility ping
, and then parse the results. Instead of doing complex regex with the entirety of ping's output, though, I also used a couple other built-in linux utilities to get just what I needed.
Here's how I got just the response time of a given IP address:
<?php
$ip_address = '123.456.789.0'; // IP address you'd like to ping.
exec("ping -c 1 " . $ip_address . " | head -n 2 | tail -n 1 | awk '{print $7}'", $ping_time);
print $ping_time[0]; // First item in array, since exec returns an array.
?>
The above script will ping the given IP address, and simply pull out the response time in milliseconds. You can further parse the response time to just be the numeric value by running it through substr($ping_time[0], 5)
.
Note that some systems ping
will output things a little differently, so you may need to adjust which line you grab (head
first grabs just the first two lines of ping
's output, tail
cuts it down to the last line of that, then awk
prints out the 7th item in the string, which is the time=
value).
Changing the awk statement to print different parts of the result will allow you to get different information, should the need arise.
Important Notes
- Note that ping times (especially) vary a bit, so this script is just helpful for general needs; if you were building a solid monitoring system, you should probably ping every second or two, then average the results, and store that value, rather than pinging just once every now and then...
- I typically avoid using
exec()
when possible, especially if writing something more general, because not every PHP installation will have access to any or all of the command line utilities used, andexec()
usage isn't often advisable if there's another way to accomplish something, but in this case, the code is going to only be used on a site running on a custom LAMP box that shouldn't ever have trouble with ping, tail, head, and awk.
Comments
Great Job Jeff! Gracias
Nice code i actually, converted this too do multi-ip queries and print out put with newlines.
One day with get away of having to hardcode each ip into script and have whitebox to query this...
Anyways thanks for the code :)
its really great work. it help's me a lot..