Skip to content

Instantly share code, notes, and snippets.

@jabez007
Created February 12, 2018 15:36
Show Gist options
  • Save jabez007/b72efb474a1d55282e1a0e46003e8d37 to your computer and use it in GitHub Desktop.
Save jabez007/b72efb474a1d55282e1a0e46003e8d37 to your computer and use it in GitHub Desktop.
Monitors your public IP address and removes your default gateway if it doesn't like what it finds
#!/bin/bash
ADDRESS="69.65.44.96" # default for my public IP now
PAUSE=1 # default to 1 second for now
# Get command line arguments
# Use -gt 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use -gt 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it).
while [[ $# -gt 1 ]]; do
key="$1"
case $key in
-a|--address)
ADDRESS="$2"
shift # past argument
;;
-p|--pause)
PAUSE="$2"
shift # past argument
;;
--default) # not used in this script
DEFAULT=YES
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
echo "Public address for VPN: $ADDRESS"
echo "Pause between address checks: $PAUSE seconds"
# Get our current Default Gateway from /sbin/route and save it
gateway="$(sudo /sbin/route -n | awk '{if ($1=="0.0.0.0") {print $2} ; q}')"
echo "$gateway" > default_gateway
# Checking that we are still accessing the internet through the VPN
while true; do
# retrieve what our public IP address is
publicIP="$(wget http://ipinfo.io/ip -qO -)"
# If we can't reach the above web page to retrieve out public IP
if [ -z $publicIP ]; then
echo "Failed to retrieve public address"
sleep 60
# does this IP match what we would expect through the VPN?
elif [ $publicIP != $ADDRESS ]; then
echo "Public address found: $publicIP"
echo "Removing Default Gateway"
# If not, then we need to pull the Default Gateway
# This is just the quick and dirty to kill the traffic out of the LAN
sudo /sbin/route del default gw "$(cat default_gateway)"
fi
# pause between iterations so we aren't needlessly spamming
sleep $PAUSE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment