Sometimes the wifi on my laptop misbehaves, or I just feel the need for some pinging. Previously, I had a simple wrapper script that called fping
with a bunch of hostnames and the IP address of my gateway. Then I thought "what about a nice bit of gawk
?" ...
Sample output
Here's some faked results. The hosts seem to be ordered by ping time (my script is not ordering them - it's just what fping
returns)
2021-10-26 20:10:03
HOST LOSS LAST
---- ---- ----
192.168.0.1 0% ok
google.com 0% ok
facebook.com 13% ok
youtube.com 100% FAIL
---
Script
Edit the list of targets to suit your requirements. Note: internationalisation not tested.
#! /bin/sh
# list of targets to ping
targets="
facebook.com
google.com
youtube.com
"
# check for required tools
command -v fping >/dev/null 2>&1 || { echo "ERROR: fping not found"; exit 1; }
command -v gawk >/dev/null 2>&1 || { echo "ERROR: gawk not found"; exit 1; }
command -v ip >/dev/null 2>&1 || { echo "ERROR: ip not found"; exit 1; }
# try to lookup our default gateway
gateway="$(ip route | gawk '/default via/{print $3}')"
# clear the screen
reset
# continuously ping and show results
fping --timestamp --period 5000 --unreach --loop --ipv4 \
$gateway \
$targets \
2>&1 | gawk '
{
if ("Unreachable" == $3) {
hostname = $11
result = "UNREACHABLE"
loss = "???"
# comment out the next line if you want to see explicit UNREACHABLE messages
next
} else {
hostname = $2
current_cycle = $4
if ("timed" == $5) {
result = "FAIL"
loss = $9
} else {
result = "ok"
loss = $11
}
}
if (current_cycle != prev_cycle) {
# clear a line to make it obvious if our end changes weirdly...
printf "---%-70s", " "
rawdate = substr($1, 2, 10)
# move cursor home and print timestamp
print "\033[0;0H", strftime("%Y-%m-%d %H:%M:%S", rawdate)
printf "%25s %4s %-10s \n", "HOST", "LOSS", "LAST"
printf "%25s %4s %-10s \n", "----", "----", "----"
prev_cycle = current_cycle
}
printf "%25s %4s %-10s\n", hostname, loss, result
}
'