Skip to content

Instantly share code, notes, and snippets.

@saidinesh5
Last active September 28, 2016 23:47
Show Gist options
  • Save saidinesh5/5b11af6c24846ff6f4a4c01470333952 to your computer and use it in GitHub Desktop.
Save saidinesh5/5b11af6c24846ff6f4a4c01470333952 to your computer and use it in GitHub Desktop.
A silly little watchdog script to monitor my wifi connection and reset it
#!/bin/sh
#Host to ping to check pingability, by default it is the default gateway
PING_HOST=$(ip route show default | awk '/default/ {print $3}')
PING_INTERVAL=10
PING_TIMEOUT=5
#Number of consecutive ping successes to note that the server is up
SUCCESS_ALERT_COUNT=1
#Number of consecutive ping failures to note that the server is down
FAILURE_ALERT_COUNT=3
function ON_PING_SUCCESS {
echo "Network is up"
}
function ON_PING_FAILURE {
echo "Network is down"
rmmod iwldvm
rmmod iwlwifi
modprobe iwlwifi
}
#--------------------------------------------------------- Implementation
_SUCCESS_COUNT=0
_FAIL_COUNT=0
_LAST_STATE=''
_CURRENT_STATE=''
echo "Starting Internet Watchdog..."
while true; do
ping -q -c 1 -W $PING_TIMEOUT $PING_HOST &> /dev/null
if [ $? -eq 0 ]; then
_SUCCESS_COUNT=`expr $_SUCCESS_COUNT + 1`
_FAIL_COUNT=0
else
_SUCCESS_COUNT=0
_FAIL_COUNT=`expr $_FAIL_COUNT + 1`
fi
if [ $_SUCCESS_COUNT -eq $SUCCESS_ALERT_COUNT ]; then
_CURRENT_STATE="SUCCESS"
fi
if [ $_FAIL_COUNT -eq $FAILURE_ALERT_COUNT ]; then
_CURRENT_STATE="FAILURE"
fi
if [[ $_CURRENT_STATE != $_LAST_STATE ]]; then
if [[ $_CURRENT_STATE == "SUCCESS" ]]; then
ON_PING_SUCCESS
else
ON_PING_FAILURE
fi
_LAST_STATE=$_CURRENT_STATE
fi
sleep $PING_INTERVAL
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment