Skip to content

Instantly share code, notes, and snippets.

@rollcat
Created May 6, 2016 08:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rollcat/3fc17b2136df512291d4bcb80a2fe749 to your computer and use it in GitHub Desktop.
Save rollcat/3fc17b2136df512291d4bcb80a2fe749 to your computer and use it in GitHub Desktop.
Script to diagnose network connectivity
#!/usr/bin/env bash
set -eu
case `uname` in
Linux)
gateway=`ip route | awk '$1~/default/ && $2~/via/ {print $3}'`
ping_opts="-c1 -W2"
;;
OpenBSD)
gateway=`route -n show | awk '$1~/default/ {print $2}'`
ping_opts="-c1 -w2"
;;
*)
echo "E: Which OS are you running, smartass?"
exit 111
;;
esac
nss=`awk '$1~/nameserver/ {print $2}' /etc/resolv.conf`
monitor="google.com google.pl google.de"
verbose=0
quiet() {
if [ "$verbose" -eq 0 ]
then
$@ >/dev/null
else
$@
fi
}
err() {
echo $@ >&2
}
for arg in "${@}"
do
case $arg in
--debug|-d)
err "D: gateway:" $gateway
err "D: nss:" $nss
err "D: monitor:" $monitor
verbose=1
;;
esac
done
if test -z "$gateway"
then
err "E: No gateway"
echo "nogw"
exit 2
else
err "I: gateway OK"
fi
if test -z "$nss"
then
err "E: No configured nameservers"
echo "nonss"
exit 2
else
err "I: nss OK"
fi
for host in $gateway $nss
do
if echo "$host" | grep -q :
then
ping=ping6
else
ping=ping
fi
if ! quiet $ping $ping_opts "$host"
then
err "E: No response from $host"
echo "$host"
exit 2
else
err "I: host OK: $host"
fi
sleep 0.1
done
for host in $monitor
do
if ! quiet ping $ping_opts "$host"
then
err "E: No response from $host"
echo "$host"
exit 2
else
err "I: host OK: $host"
fi
sleep 0.1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment