May 15, 2015

Script To Automatically Connect and Reconnect VPN in Ubuntu 15.04

This is an update to my post I did a few years ago on how to make a script that will automatically keep you connected to VPN. You would think that it would still work in Ubuntu 15.04, but then you would be wrong.

There are a few reasons for this:

  • For some reason Ubuntu 15.04 doesn't like people adding random scripts to init.d anymore. You get errors about lsb tags and overrides.
  • nmcli changed their command line options. Namely status is now show. (Why? Who knows?!)
  • Since we are no longer using /etc/init.d you don't need to add your password to the VPN file in  /etc/NetworkManager/system-connections/. You also don't have to change the password flags.
  • Network-manager-pptp package has a bug in it where it doesn't like to connect to PPTP VPN connections and errors out.
Ok, so for the first item, I got around the /etc/init.d issue by creating my autovpn script and placing it in my home directory. I then went into Applications > System Tools > Preferences > Startup Applications and added my script in there. When I reboot and login, the script is launched in the background and keeps me connected to VPN!



For the second item, your script should look like this:

#!/bin/bash
while [ "true" ]
do
    VPNCON=$(nmcli con show uuid d42cc2a7-a582-418c-a018-188b89fbb471 | grep VPN.VPN-STATE | awk '{print $2}')
    if [[ $VPNCON != "5" ]]; then
        echo "Disconnected, trying to reconnect..."
        (sleep 1s && nmcli con up uuid d42cc2a7-a582-418c-a018-188b89fbb471)
    else
        echo "Already connected !"
    fi
    sleep 20
done

The only difference from my original script is changing status to show. You can read the original post on how to find the UUID of your VPN connection.

Finally, to fix the issue with network-manager-pptp I had to create a file called  /etc/modules-load.d/netfilter.conf. I did that by running:
#sudo nano /etc/modules-load.d/netfilter.conf
I then added the following lines:
nf_nat_pptp
nf_conntrack_pptp
nf_conntrack_proto_gre
After I saved that file I rebooted and VPN connected fine.

For the record, here are some of the errors I was getting before I created that file:

May  6 20:33:14 Media-Server NetworkManager[753]: ** Message: nm-pptp-ppp-plugin: (nm_exit_notify): cleaning up
May  6 20:33:14 Media-Server NetworkManager[753]: ** (nm-pptp-service:11430): WARNING **: pppd exited with error code 16
May  6 20:33:14 Media-Server NetworkManager[753]: <warn> VPN plugin failed: connect-failed (1)
May  6 20:33:14 Media-Server NetworkManager[753]: <info> devices removed (path: /sys/devices/virtual/net/ppp0, iface: ppp0)
May  6 20:33:14 Media-Server NetworkManager[753]: <warn> VPN plugin failed: connect-failed (1)
May  6 20:33:14 Media-Server NetworkManager[753]: <warn> VPN plugin failed: connect-failed (1)
May  6 20:33:14 Media-Server NetworkManager[753]: <info> VPN plugin state changed: stopped (6)
May  6 20:33:14 Media-Server NetworkManager[753]: <info> VPN plugin state change reason: unknown (0)
May  6 20:33:14 Media-Server NetworkManager[753]: <warn> error disconnecting VPN: Could not process the request because no VPN connection was active.

It took me a while to sort this out, and it wasn't very fun. Hopefully it will help you! If it does, let us know in the comments.



Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | stopping spam