Last active
May 19, 2018 19:45
-
-
Save hlapp/0b33fba22cf0b219ec28215ecd5c9754 to your computer and use it in GitHub Desktop.
Test whether the network connection to the local gateway is up, and if not, reset the network interface, and if necessary also avahi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Tests whether the network connection to the local router is up. If not, | |
# resets the network interface that was supposed to be connected. After | |
# that tests whether multicast DNS resolution of the localdomain hostname | |
# is still working, and if not, resets the avahi daemon as well. | |
# | |
# Hilmar Lapp <hlapp@drycafe.net> | |
# | |
# To the extent possible under law, I have waived all copyright and related | |
# or neighboring rights to this work under the CC0 1.0 Universal Public Domai | |
# Dedication. See https://creativecommons.org/publicdomain/zero/1.0 | |
# log message level | |
# | |
# Log to standard output if in a terminal, and to syslog otherwise. Level | |
# defaults to info. | |
function log() { | |
level=$2 | |
if [ -z "$level" -a ! -t 1 ] ; then | |
level=info | |
fi | |
if [ -t 1 ] ; then | |
if [ -n "$level" ] ; then | |
level="${level}: " | |
fi | |
echo "$level$1" | |
else | |
logger -p local0.$level -t "network-watch" "$1" | |
fi | |
} | |
# determine the default gateway router for internet traffic | |
gw=$(ip route list scope global | cut -d" " -f3) | |
# if there's no gateway then we're not online | |
if [ -z "$gw" ] ; then | |
online=1 | |
else | |
# most routers have an HTTP server, see whether we can connect to the port | |
nc -z -w 3 $gw 80 >/dev/null 2>&1 | |
# success (exit status zero) means we're online | |
online=$? | |
fi | |
if [ $online -ne 0 ] ; then | |
# determine the network interface that was supposed to be connected | |
dev=$(ip route list scope global | cut -d" " -f5) | |
# if there's no device then there's no point in continuing | |
if [ -z "$dev" ] ; then | |
log "No network device for global route. Exiting." error | |
exit 1 | |
fi | |
log "Failed to reach gateway $gw through interface $dev, resetting it" error | |
# cycle the network interface | |
ifdown --force $dev | |
sleep 15 | |
ifup $dev | |
# Sometimes, mDNS names continue to hang around after cycling the interface, | |
# resulting in a false hostname conflict when avahi tries to advertise the | |
# zeroconf name again. Therefore, after a minute test whether our IP address | |
# resolves correctly through multicast DNS, and if not, restart avahi. | |
sleep 60 | |
addr=$(ip -4 addr show $dev | grep inet | perl -n -e '/inet\s([0-9.]+)/ && print $1;') | |
mname=$(dig +short +time=2 -x $addr @224.0.0.251 -p 5353) | |
hname=$(hostname) | |
if [ "$mname" = "${hname}.local." ] ; then | |
log "mDNS correctly resolves ${hname}.local" | |
else | |
log "mDNS fails to correctly resolve ${hname}.local, restarting avahi" error | |
service avahi-daemon stop | |
sleep 30 | |
service avahi-daemon start | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment