Bandwidth-Aid: A VPN disconnect reminder script
Mike Ackerman, Former Senior Developer
Article Category:
Posted on
Do you often forget to disconnect from a VPN connection? Try this out for a handy reminder!
VPNs are great for allowing you to do certain things that require being on your company's network while you're on an external internet connection. At Viget, we use a VPN for a number of activities: connecting to internal servers, viewing secure internal websites, deploying the latest code to certain servers, etc.
Recently, while working from home, I kept noticing that my internet felt slow. I'd hop on over to speedtest.net and see much lower than expected results. Before calling up my ISP, I noticed that I had forgotten to disconnect from the VPN! Connected for 8-15 hours, routing all of my traffic through our company's private network! :(
So, I came up with a solution: a small ruby script that runs every 10 minutes to check whether I'm connected to the VPN, and if I have been for more than 10 minutes, yell at me.
Note: my solution will only work on OSX, as it uses say and osascript for creating a dialog box notification
Steps to using it:
- 1. Install Ruby (I recommend homebrew or rbenv)
- 2. Save the contents of the script (found below) in a file in your home directory:
/home/username/vpn_status_reminder.rb
-
Script contents:
-
# /home/username/vpn_status_reminder.rb
if connected_to_vpn?
if already_connected?
send_notification
else
set_connected_status
end
else
if already_connected?
set_disconnected_status
end
end
BEGIN {
require 'fileutils'
VPN_CONNECTED_FILENAME = "./VPN_CONNECTED.txt"
def ifconfig_output
@ifconfig_output ||= `/sbin/ifconfig`.strip
end
def connected_to_vpn?
ifconfig_output.include?("ppp0")
end
def set_connected_status
FileUtils.touch(VPN_CONNECTED_FILENAME)
end
def already_connected?
File.exist?(VPN_CONNECTED_FILENAME)
end
def set_disconnected_status
File.delete(VPN_CONNECTED_FILENAME)
end
def send_notification
`say -v Zarvox "You are connected to the VPN"`
`osascript -e 'tell app "Finder" to display dialog "\u{203C}\u{1F6A8}\u{203C} Connected to VPN \u{203C}\u{1F6A8}\u{203C}"'`
end
}
- 3. In your terminal, modify your cron scheduled jobs:
- a. Determine where your ruby executable is:
which ruby
- b. Write down or store this value excluding the last
/ruby
part. example: if your output is/usr/bin/ruby
the result would be/usr/bin
- c. Update your cron jobs:
EDITOR=nano crontab -e
hit Enter and paste the following content:*/10 * * * * /bin/bash -c 'export PATH=INSERT_HERE:$PATH; cd $HOME/; ruby vpn_status_reminder.rb'
, followed byctrl+x
,Y
,Enter.
WhereINSERT_HERE
should be replaced with the result of step 3.b.
- a. Determine where your ruby executable is:
Do you like this solution or have any ideas for making it better? Let me know in the comments below!