Skip to content

Instantly share code, notes, and snippets.

@dreamon11 dreamon11/watchdog.sh
Last active Apr 8, 2018

Embed
What would you like to do?
openwrt watchdog: interface auto reconnect
#!/bin/sh
ip1=8.8.8.8
ip2=208.67.222.222
tries=3
if [[ -z $1 ]]; then
logger -t watchdog "Missing interface name"
exit 1
fi
interface=$1
ifname="$(uci get network.$interface.ifname 2> /dev/null)"
if [[ $? -eq 1 ]]; then
logger -t watchdog "ifname not found in '$interface' interface"
exit 1
fi
while [ $tries -ge 0 ]; do
if /bin/ping -c 1 $ip1 -I $ifname > /dev/null 2>&1; then
exit 0
fi
if /bin/ping -c 1 $ip2 -I $ifname > /dev/null 2>&1; then
exit 0
fi
sleep 3
tries=$((tries-1))
done
logger -t watchdog "Physical network interface '$ifname' is down: restart interface '$interface'"
ifdown $interface && ifup $interface
sleep 60
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.