Skip to content

Instantly share code, notes, and snippets.

@pyksid
Last active January 5, 2022 12:31
Show Gist options
  • Save pyksid/dc35c659a594ee5e51d74d028e9ce988 to your computer and use it in GitHub Desktop.
Save pyksid/dc35c659a594ee5e51d74d028e9ce988 to your computer and use it in GitHub Desktop.
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