Know your ping

Discussion and support for all Linux distributions and Unix flavours (FreeBSD, OpenBSD, etc).
Post Reply
Kasyx
Registered User
Posts: 139
Joined: 13 Dec 2006, 02:00

Know your ping

Post by Kasyx »

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.
Richard_
Registered User
Posts: 2295
Joined: 18 May 2003, 02:00
Location: Durban, South Africa

Re: Know your ping

Post by Richard_ »

Google isn't all-powerful, and also has its occasional hickups, not to mention that pinging Google is hardly the most accurate measurement of latency especially, and bearing in mind that Google handles millions of requests per second your ping results will be very erratic. Rather ping two or three local and international servers, and then take the lowest of each result. This should give you a far more realistic representation of latency. Otherwise, cool script, if you have use for such a thing, and more than one ADSL account to play around with :P
Kasyx
Registered User
Posts: 139
Joined: 13 Dec 2006, 02:00

Re: Know your ping

Post by Kasyx »

Of course pinging Google isn't the best way to go about it, but it makes for a decent example, I guess. Generally I will ping the server of the game I happen to be addicted to at the time, be it WoW, Guild Wars or EVE.

What I have been looking into recently is a script that, much like RouteSentry, splits your bandwidth. However, rather than splitting local and international down two accounts, it would split shaped and unshaped. Thus you can set up all your WoW/EVE/etc. traffic to run down the unshaped account, and then have general browsing and downloading going down your regular, shaped account. Maybe I will actually get around to writing something like that one day. Right now I have no gaming PC and am stuck using my crappy laptop, so I don't really have the inspiration required right now.
Richard_
Registered User
Posts: 2295
Joined: 18 May 2003, 02:00
Location: Durban, South Africa

Re: Know your ping

Post by Richard_ »

You can use Quagga / Zebra on a Linux box for routing by IP or port. Probably the easiest way to do it. Problem with RouteSentry is that it only runs on Windoze and only on the PC on which you want to do the splitting.

That's actually quite a good idea; mostly people only bother splitting local and international, but splitting by port would be an interesting application too.
Post Reply