Skip to content

Instantly share code, notes, and snippets.

@sehrgut
Last active December 21, 2015 13:58
Show Gist options
  • Save sehrgut/6315879 to your computer and use it in GitHub Desktop.
Save sehrgut/6315879 to your computer and use it in GitHub Desktop.
Useful tool to keep tabs on an iffy network connection.
#!/bin/bash
# Licensed under any of GPLv2, MIT, and BSD 2-clause licenses
TEST_ADDR='4.2.2.1'
INTERVAL=10
VERSION='nettest v0.2a, keith beckman'
args=$(getopt vhi:H:V $*)
function usage () {
cat <<EOF
Usage: nettest [-H host_addr][-i seconds][-hv][-V]
nettest pings a host (default 4.2.2.1) at intervals (default 10s) and reports
if the network is down. If -V (verbose mode) is enabled, it will print a dot
every time it successfully pings.
EOF
exit 2
}
if [ $? != 0 ]; then
usage
fi
set -- $args
for i; do
case "$i" in
-H)
TEST_ADDR="$2"
shift;;
-i)
INTERVAL=$2
shift;;
-v)
echo $VERSION
exit 2;;
-V)
VERBOSE=1;;
-h)
usage;;
esac
shift
done
function isUp() {
ping -Q -c 1 "$TEST_ADDR" > /dev/null 2>&1
return $?
}
while true; do
if isUp; then
if [[ $VERBOSE ]]; then
printf "."
fi
else
printf "\nERROR: (%s) Network down\n" "`date`"
fi
sleep "$INTERVAL"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment