Reconnecting Ubiquiti OpenVPN connections
I have owned a Ubiquiti EdgeRouter Lite for more than a year, and continue to be impressed at its versatility. It’s three gigabit ports that you can do almost any conceivable thing with. Currently I am using it to route all of my internet traffic through a VPN with Private Internet Access. This is straightforward to set up, but can be annoying for me (and even worse, the people I live with who don’t have ssh), when the connection is dropped. A quick reboot
is enough; but I wanted something automated.
Setup
Currently I have my config files in /config/auth/pia/melbourne2.ovpn
. I created a shell script in /config/auth/pia/restart.sh
:
#!/bin/bash
curl -s --connect-timeout 5 http://google.com > /dev/null
if [[ $? -eq 0 ]]; then
echo "Online"
else
pkill -f /usr/sbin/openvpn
/usr/sbin/openvpn --daemon --verb 3 --writepid /var/run/openvpn-vtun0.pid --status /var/run/openvpn/status/vtun0.status 30 --config /config/auth/pia/melbourne2.ovpn --dev-type tun --dev vtun0
fi
chmod +x restart.sh
will make this file executable.
This script looks to see if Google is contactable from the router, and uses this test to determine if the VPN is offline. If vtun0 is determined to be offline, then the openvpn process is killed, and restarted.
The router does have a a facility to set up scheduled tasks, but to me it was a little opaque. Login to your router as root, and edit the file /etc/crontab
. Add the following line below the others:
* * * * * root /config/auth/pia/restart.sh
The restart.sh
script will be run every minute. Next time your internet drops out, you VPN connection should be re-established around a minute or so after your internet is.