Check VPN connectivity

Ultimately I am trying to figure out why my Ubuntu server routinely craps out and is unable to reach the outside world. I find that if I restart the system, networking comes back up ok…at least for a while. This is an first crack at a rudimentary script to reboot it if unable to connect (or if the VPN isn’t working).

Eventually I will want to add to this and make it a little more robust.  I should l also address WHY this happens, but until I have the time to dig into that I will work around it in the ultimate/windows fashion:  reboot!  I have found that when the system is in this state, simply restarting the network has no effect.  So my hand is forced.

I will use dnsleaktest.com to determine if the connection is working.  If nothing comes back from my curl command I know that the connection is mucked up (or that DNS isn’t resolving correctly…maybe I should look into that first?).  I also check to see if my IP is not actually from the location I’ve configured my VPN for, but again, I’m not sure how relevant that is because I think the bigger problem is that I don’t have a connection.

Obviously I will not be able to send an email in this state, but I attempt to write to a file that can then be included in an email upon reboot.  This is probably worth a different post altogether, but that is why I send the messages to /root/reboot.log (yet another thing that could use some improvement).  Enough excuses for my being lazy, here is the “script:”

location=$(curl -ks https://www.dnsleaktest.com | grep -A1 Hello);

if [[ -z $location ]] || $(echo $location | grep -i Raleigh); then
  /etc/update-motd.d/50-landscape-sysinfo | \
    sed -e '/Graph/d' -e '/canonical/d' >> /root/reboot.log;
  echo "[$location]" >> /root/reboot.log;
  /sbin/reboot;
fi;

exit;

UPDATE:  2016-03-05
I actually have had this running since January.  Since January, this script has rebooted my system 34 times.  That seems like a lot and I should probably analyze it more deeply.  But the point of this update is to state that in those 34 times, not a single reboot has been because of the VPN failing and having my IP be from Raleigh.  I could probably remove that logic, but as of this writing I will only update the script to remove that from my output.
I’ve also added a test to see if Plex was running in this post.

I want to run this regularly so I’ve saved it as /root/check_vpn.sh and put it in root’s crontab to check every 10 minutes:

*/10 * * * * /root/check_vpn.sh

That’s it for now.  I hope to make this (and myself) smarter.


WHAT I LEARNED:

  • In a cron job, it is essential to put the entire path to the commands, particularly the reboot command

 

REFERENCES:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *