Skip to content

Instantly share code, notes, and snippets.

@charsi
Last active October 20, 2020 08:02
Show Gist options
  • Save charsi/fa99118a61cb9263bc6affc2c7352609 to your computer and use it in GitHub Desktop.
Save charsi/fa99118a61cb9263bc6affc2c7352609 to your computer and use it in GitHub Desktop.
shell script for monitoring WAN staus on router and sending pushover notifications
#!/bin/sh
# This script checks for internet connectivity on your wan port and sends a pushover message if there has been a change.
# Multiple WAN ports supported.
# Based on Scripts from Wob_76 & WaLLy3K at http://www.linksysinfo.org/index.php?threads/wan-up-down-notifications-script-multiwan.73057/
# Set the Number of WANS (Valid Numbes 1 to 4)
WANS=2
# Name your ISPs
WAN1_name="Excitel"
WAN2_name="Airtel"
# set pushover credentials
po_token="xxx"
po_user="xxx"
# get the gateway IP for pppoe or dhcp connection
get_gateway() {
local gw=$(nvram get wan${wannum}_gateway_get)
if [ "$gw" != "0.0.0.0" ]; then
echo $gw
else
echo $(nvram get wan${wannum}_gateway)
fi
}
notify_admin() {
# get the gateway address
wan_gateway=$(get_gateway)
# ping the gateway to test connectivity
ping_test=$(ping -c1 -W1 -w3 "$(echo $wan_gateway)" &> /dev/null; echo $?)
# update message, priority and log based on the result of the test
if [ "$ping_test" -ne 0 ]; then
logger -st "[INFO]" "$isp_name offline"
msg="$isp_name has gone offline!"
p="1"
wan_status="offline"
else
logger -st "[INFO]" "$isp_name online"
msg="$isp_name is now online :)"
p="0"
wan_status="online"
fi
# set file name for saving connection status
WAN_STATUS_FILE=/tmp/wan${wannum}_status
# get last connection status, if file exists
if [ -f $WAN_STATUS_FILE ]; then
WAN_LAST_STATUS=$(cat $WAN_STATUS_FILE)
else
WAN_LAST_STATUS=
fi
# if status has changed, send notification
if [ $wan_status != "$WAN_LAST_STATUS" ]; then
echo $wan_status > $WAN_STATUS_FILE
# send notification via pusher
push_msg=$(curl -s -F "token=$po_token" -F "user=$po_user" -F "message=$msg" \
-F "title=$HOSTNAME Router" -F "priority=$p" \
-F "retry=300" -F "expire=600" http://api.pushover.net/1/messages.json)
push_status=$(echo "$push_msg" | grep -c "status\":1")
# log error if unable to send notification
if [ "$push_status" -ne 1 ]; then
logger -st "[ERROR]" "Unable to send notification via Pushover"
push_error=$(echo "$push_msg" | sed 's/^.*\("errors":.*"]\).*$/\1/' | cut -d\" -f4)
[ -n "$push_error" ] && logger -st "[ERROR]" "$push_error"
fi
fi
}
for num in $(seq 1 $WANS); do
if [ ${num} == 1 ]; then
wannum=""
else
wannum=${num}
fi
# set isp name
eval isp_name="\$WAN${num}_name"
notify_admin
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment