Living in the bandwidth-starved, latency-obsessed country that is South Africa, I took it upon myself to write a little script that sits on my router/firewall box in the study which keeps track of my local/international pings. This is not only useful to keep me notified as to my current latency, but also it is a vital part of another script I am working on (one which juggles my various ADSL accounts based on international ping thresholds which I will reveal to you once I actually get down to writing it).
Okay, this is actually more than one script (and a few external "variable" files)... And a crontab entry. It may be ugly, but it does exactly what I need it to.
Firstly:
Code: Select all
#!/bin/bash
#Pingage
#Pings local and international sites and writes the output to /etc/pingage
#Kasyx
#20080429
ping -c 4 google.com | awk -F"/" '{ if (NF > 1) {print $5} }' > /etc/pingage/international
ping -c 4 saix.net | awk -F"/" '{ if (NF > 1) {print $5} }' > /etc/pingage/local
What the above script effectively does, is pings google.com, and then writes the output (x ms) to a file (in /etc/pingage/international) which can now be read by other scripts (perhaps not the most elegant way to do it, but it does what I need - feel free to edit as you will). It does the same for local (I don't use saix.net, I use my company's web server, but to each his own...). Now you need to add this script to the crontab:
crontab -e (to edit your crontab, for those of you unfamiliar with it)
Code: Select all
* * * * * /usr/local/bin/pingage.sh
This will run the script every minute, ensuring that average local/international pings are as up to date as possible. Now to access these pings with another script (and a few lines in the bashrc for a nice MOTD):
Code: Select all
#!/bin/bash
#Current Ping
#Displays current local and international ping
#Kasyx
#20080429
LOCPING=`cat /etc/pingage/local`
INTPING=`cat /etc/pingage/international`
echo "Current local ping is $LOCPING ms"
echo "Current international ping is $INTPING ms"
Now what this script does is fetch the current "variables" in the local/international output files and shows them to you in a more readable way. I find adding an alias to this script (pingme) is also quite helpful:
kasyx@hephaestus ~ $ pingme
Current local ping is 119.603 ms
Current international ping is 367.953 ms
You can also reference this script in your bashrc profile, however I will write a script dedicated to cool bashrc stuff.
Newbie note: You will need to manually create the folder /etc/pingage, as well as the two files "local" and "international":
mkdir /etc/pingage
touch /etc/pingage/local
touch /etc/pingage/international
Happy pinging.