VPN Auto Reconnect

Connection sharing, Firewall, Samba..etc
Forum rules
Before you post please read this

VPN Auto Reconnect

Postby Joltz on Wed Mar 14, 2012 4:24 pm

Hello,
I am running OpenVPN on Mint 12 and it works great. One annoyance though is that when I log out and log back in, it does not automatically reconnect to the VPN. Does anyone know how to resolve this?
Joltz
Level 1
Level 1
 
Posts: 7
Joined: Sat Feb 18, 2012 6:05 pm

Linux Mint is funded by ads and donations.
 

Re: VPN Auto Reconnect

Postby SiKing on Wed Mar 14, 2012 6:49 pm

In the network manager, where you defined the VPN profile, there is a checkbox "connect automatically" ... which should do the trick. I say "should" because on my system, this checkbox is grayed out.
SiKing
Level 5
Level 5
 
Posts: 575
Joined: Mon Sep 29, 2008 10:57 pm
Location: Las Vegas

Re: VPN Auto Reconnect

Postby Joltz on Thu Mar 15, 2012 1:44 pm

Thanks SiKing. i tried checking the box, but when I close and open my netbook it's still giving me the same problem. I heard this was a common bug with OpenVPN.
Joltz
Level 1
Level 1
 
Posts: 7
Joined: Sat Feb 18, 2012 6:05 pm

Re: VPN Auto Reconnect

Postby SiKing on Thu Mar 15, 2012 5:11 pm

I don't have problem with this, but I am running LM9. Way too many "small" issues on LM12 for me. :shock:
SiKing
Level 5
Level 5
 
Posts: 575
Joined: Mon Sep 29, 2008 10:57 pm
Location: Las Vegas

Re: VPN Auto Reconnect

Postby Objekt on Fri Mar 16, 2012 10:52 am

I'm interested in this topic as well, for a slightly different reason.

I have a machine on my LAN running Linux Mint 12 that I want to *only* use via VPN. That is, I need it to never connect "normally."

My problem is a little different, in that I don't sign in and out of the machine constantly. It's a desktop I leave on just about all the time. I would like VPN to be the only way it connects to the Internet, at all times.

Unfortunately, my VPN service drops the connection at times, in an unpredictable fashion. Sometimes it will stay connected for days, but other times, only for hours. I do have the "Connect Automatically" box checked in the VPN connection's settings, but it does not connect automatically.

update: A little searching found this page, which is about Ubuntu but should work for Mint, since Mint also uses NetworkManager:

https://help.ubuntu.com/community/VPNClient

Specifically, this part:
You can easily make the network manager applet start on log-in by adding the command nm-applet to your sessions. (Under System->Preferences->Session by default) However, this doesn't mean your configured connection fires up too. To make this happen you can add another command to your session startup programs:
<blah blah blah>

The command listed there (/usr/lib/network-manager-vpnc/nm-vpnc-auth-dialog -s <service_name> -n <connection_name> ) doesn't work, at least partly because I don't have any such folder as "/user/lib/network-manager-vpnc." Just /usr/lib/network-manager. I haven't yet figured out how to make the VPN connect automatically.

I also need a way to automatically attempt to re-connect, whenever the VPN service randomly drops the connection. Not just at login. I can't sit by the computer 24/7 to make sure the VPN connection stays connected.
Objekt
Level 1
Level 1
 
Posts: 31
Joined: Mon Dec 05, 2011 4:58 pm

Re: VPN Auto Reconnect

Postby ziphem on Sat Apr 14, 2012 12:33 am

Hi,

Perhaps this will help?

http://ubuntuforums.org/showthread.php?t=1316314

While I used this on my Fedora machine - I don't have my Mint machine booted at the moment so can't check if this is even valid there - I stuck a file with the code from the above post (file named "autovpn") in "/etc/NetworkManager/dispatcher.d/" . Connecting to the network, I get logged into the VPN automatically. When I disconnect my VPN manually, it attempts to auto-reconnect.

I hope this works for you, post back if it does.
ziphem
Level 1
Level 1
 
Posts: 2
Joined: Sat Apr 14, 2012 12:18 am

Re: VPN Auto Reconnect

Postby Objekt on Thu Apr 26, 2012 9:57 am

Hey, thanks! I got it working!

For the reference of other Mint users, here is the condensed version of the Ubuntu Forums thread linked above. I'll make it as easy as I can.

1) Start a text editor with elevated priveleges. For example, enter at the command prompt:
Code: Select all
gksudo gedit

You need your editor running with elevated privileges because you will be saving a file in a folder that you can't make changes to with a "normal" access level.

2) Create a file called "autovpn" and save it in the directory mentioned above (/etc/NetworkManager/dispatcher.d). In this "autovpn" file, place the following code:
Code: Select all
#!/usr/bin/python

import sys
import os
import dbus
import gobject
from  dbus.mainloop.glib import DBusGMainLoop

# The uuid of the VPN connection to activate
VPN_CONNECTION_UUID = "FILL IN YOUR OWN"

# The uuid of the connection that needs to be active to start the VPN connection
ACTIVE_CONNECTION_UUID = "FILL IN YOUR OWN"

# some service, path and interface constants
NM_DBUS_SERVICE                   = "org.freedesktop.NetworkManager"
NM_DBUS_PATH                      = "/org/freedesktop/NetworkManager"
NM_DBUS_INTERFACE                 = "org.freedesktop.NetworkManager"
NM_DBUS_IFACE_CONNECTION_ACTIVE   = "org.freedesktop.NetworkManager.Connection.Active"
NM_DBUS_SERVICE_SYSTEM_SETTINGS   = "org.freedesktop.NetworkManagerSystemSettings"
NM_DBUS_SERVICE_USER_SETTINGS     = "org.freedesktop.NetworkManagerUserSettings"
NM_DBUS_IFACE_SETTINGS            = "org.freedesktop.NetworkManagerSettings"
NM_DBUS_PATH_SETTINGS             = "/org/freedesktop/NetworkManagerSettings"
NM_DBUS_IFACE_SETTINGS_CONNECTION = "org.freedesktop.NetworkManagerSettings.Connection"

DBusGMainLoop(set_as_default=True)

nm_dbus_settings_services = (NM_DBUS_SERVICE_SYSTEM_SETTINGS, NM_DBUS_SERVICE_USER_SETTINGS)

def get_connections(bus, service):
  proxy = bus.get_object(service, NM_DBUS_PATH_SETTINGS)
  iface = dbus.Interface(proxy, dbus_interface=NM_DBUS_IFACE_SETTINGS)
  return iface.ListConnections()

def get_connection_by_uuid(bus, uuid):
  for service in nm_dbus_settings_services:
    for c in get_connections(bus, service):
      proxy = bus.get_object(service, c)
      iface = dbus.Interface(proxy, dbus_interface = NM_DBUS_IFACE_SETTINGS_CONNECTION)
      settings = iface.GetSettings()
      if settings['connection']['uuid'] == uuid:
        return (c, service)
  return None

def list_uuids(bus):
  for service in nm_dbus_settings_services:
    for c in get_connections(bus, service):
      proxy = bus.get_object(service, c)
      iface = dbus.Interface(proxy, dbus_interface=NM_DBUS_IFACE_SETTINGS_CONNECTION)
      settings = iface.GetSettings()
      conn = settings['connection']
      print " %s: %s - %s (%s)" % (service, conn['uuid'], conn['id'], conn['type'])

def get_active_connection_path(bus, uuid):
  proxy = bus.get_object(NM_DBUS_SERVICE, NM_DBUS_PATH)
  iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
  active_connections = iface.Get(NM_DBUS_INTERFACE, 'ActiveConnections')
  connection_and_service = get_connection_by_uuid(bus, uuid)
  if connection_and_service == None:
    return None
  for a in active_connections:
    proxy = bus.get_object(NM_DBUS_SERVICE, a)
    iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
    path = iface.Get(NM_DBUS_IFACE_CONNECTION_ACTIVE, 'Connection')
    service = iface.Get(NM_DBUS_IFACE_CONNECTION_ACTIVE, 'ServiceName')
    if service != connection_and_service[1]:
      continue
    proxy = bus.get_object(connection_and_service[1], path)
    iface = dbus.Interface(proxy, dbus_interface=NM_DBUS_IFACE_SETTINGS_CONNECTION)
    settings = iface.GetSettings()
    if settings['connection']['uuid'] == uuid:
      return a
  return None

def activate_connection(bus, vpn_connection, active_connection):
  def reply_handler(opath):
    print "<<SUCCESS>>"
    sys.exit(0)
  def error_handler(*args):
    print "<<FAILURE>>"
    sys.exit(1)
  proxy = bus.get_object(NM_DBUS_SERVICE, NM_DBUS_PATH)
  iface = dbus.Interface(proxy, dbus_interface=NM_DBUS_INTERFACE)
  iface.ActivateConnection(NM_DBUS_SERVICE_USER_SETTINGS,
                           vpn_connection[0],
                           dbus.ObjectPath("/"),
                           active_connection,
                           reply_handler=reply_handler,
                           error_handler=error_handler)

bus = dbus.SystemBus()

#print "connections:"
#list_uuids(bus)

if len(VPN_CONNECTION_UUID) < 1 or len(ACTIVE_CONNECTION_UUID) < 1:
    print "you need to set the uuids"
    sys.exit(0)

vpn_connection = get_connection_by_uuid(bus, VPN_CONNECTION_UUID)
if not vpn_connection:
    print "Configured VPN connection is not known to NM, check VPN_CONNECTION_UUID."
    sys.exit(1)

active_connection = get_connection_by_uuid(bus, ACTIVE_CONNECTION_UUID)
if not active_connection:
  print "Configured active connection is not known to NM, check ACTIVE_CONNECTION_UUID."
  sys.exit(1)

if get_active_connection_path(bus, VPN_CONNECTION_UUID) != None:
  print "VPN connection already activated"
  sys.exit(0)

active_connection_path = get_active_connection_path(bus, ACTIVE_CONNECTION_UUID)
if not active_connection_path:
  print "The required connection isn't active at the moment"
  sys.exit(0)

print "connecting...." # to:\n  '%s'\nwith active connection:\n  '%s'" % (vpn_connection, active_connection)

activate_connection(bus, vpn_connection, active_connection_path)

loop = gobject.MainLoop()
loop.run()

(Note this isn't my work and I don't take credit. Just saving you the trouble of digging through the Ubuntu forums.)

3) Go to the command prompt. Run the following command:
Code: Select all
nmcli con status

Make note of the UUID values given, e.g. copy & paste them into a text file.

4) Back to the autovpn script: Fill in UUIDs for both the VPN connection you want to use, and the connection that has to be active before you can connect to VPN.

For most users, the second one is your default network connection, usually a wired Ethernet connection.

Note that the UUID values will be unique to your system. They shouldn't change from login to login, however, or this script wouldn't work. For your reference, here's what I see:
Code: Select all
objekt@linuxmint12 ~ $ nmcli con status
NAME                      UUID                                   DEVICES    DEFAULT  VPN 
Wired connection 1        e9908d28-ffec-41cd-babb-c0cefb38ae6a   eth0       yes      no   
VPN Connection            699f17f2-0ab0-4d1d-94d3-24136ef81603   --         no       yes 


5) Save "autovpn" file.

6) Log out and log back in or reboot. Your VPN connection should activate as soon as your regular network connection is on.

Next step: implement the script to automatically detect whether VPN is still connected, and re-connect if not.

Once I have the procedure fully worked out, this thread should be tacked or otherwise made easy to find. Automatic VPN connection is a basic function, and sorely missing from a default install of Mint.
Objekt
Level 1
Level 1
 
Posts: 31
Joined: Mon Dec 05, 2011 4:58 pm

Re: VPN Auto Reconnect

Postby gorade on Sun Apr 29, 2012 12:09 pm

Note that the UUID values will be unique to your system. They shouldn't change from login to login, however, or this script wouldn't work. For your reference, here's what I see:



The UUID-value of the wired connection keeps changing from every new login, while the VPN-connection UUID seems the same.
Thus, it doesn't work for me.
User avatar
gorade
Level 1
Level 1
 
Posts: 42
Joined: Sat Feb 07, 2009 3:53 pm

Re: VPN Auto Reconnect

Postby Objekt on Sun Apr 29, 2012 7:56 pm

That does seem to be the weak spot in this method. The code isn't very robust, as it will fail if any of the UUIDs change.
Objekt
Level 1
Level 1
 
Posts: 31
Joined: Mon Dec 05, 2011 4:58 pm

Re: VPN Auto Reconnect

Postby gorade on Tue May 01, 2012 12:57 am

Think I understand why it doesn't work in my case. The VPN-channel is to an anonymizer, the purpose of which is to change all the time.
User avatar
gorade
Level 1
Level 1
 
Posts: 42
Joined: Sat Feb 07, 2009 3:53 pm

Re: VPN Auto Reconnect

Postby Objekt on Mon May 07, 2012 12:16 am

It stopped working for me, too.

I have no idea why. Neither of the UUIDs changed (LAN and VPN connections).
Objekt
Level 1
Level 1
 
Posts: 31
Joined: Mon Dec 05, 2011 4:58 pm

Re: VPN Auto Reconnect

Postby ziphem on Fri May 11, 2012 3:03 am

For someone like gorade, who has the issue that his (/her?) UUID changes on each log-in and so this script is of little use, this code should be modified so that when the Active Connection UUID is *not* the specified UUID, it auto-connects to the VPN. You could set in a dummy VPN then and it'll connect automatically. A modification of the code in that way would also be very useful to those who do not need their VPN (i.e., WiTopia) when they are at their home network; in this manner, it would connect to the VPN when the UUID is not that of their home network, i.e., such as when in a hotel, airport, etc.

I'm not familiar with Python and really don't have a good idea of how to modify the script as such. If anyone can make a go of it, that would be fantastic.
ziphem
Level 1
Level 1
 
Posts: 2
Joined: Sat Apr 14, 2012 12:18 am

Re: VPN Auto Reconnect

Postby Objekt on Fri May 11, 2012 11:44 am

Exactly, it is not robust code. Hard-coding what should be a variable almost always ends in tears.

Unfortunately, I too lack the knowledge required to fix the script.

There is more going on here, however. I double-checked my UUID's. They do not change, yet the script fails to do anything - sometimes. Other times, it works. Very strange.
Objekt
Level 1
Level 1
 
Posts: 31
Joined: Mon Dec 05, 2011 4:58 pm

Re: VPN Auto Reconnect

Postby kimboinatl on Sun Jan 13, 2013 11:56 am

Hey guys,

I realize that this is probably not the most elegant solution, but here's what I did.

I created this script to check to see if my VPN connection was up every 30 seconds. If it isn't it restarts it. Replace "Private Internet Access" with whatever your VPN connect is called:

Code: Select all
#!/bin/bash

while [ "true" ]
do
   VPNCON=$(nmcli con status)
   if ! grep -q "Private Internet Access" <<<$VPNCON; then
      echo "`date` Disconnected, trying to reconnect..."
      sleep 1
      nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
   fi
   sleep 30
done


I added this to my /etc/rc.local file so that it starts at bootup:

Code: Select all
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#VPN reconnect script, to ensure VPN is always on
nohup /root/scripts/reconnectvpn.sh > /var/tmp/reconnectvpn.log 2>&1 &

exit 0


Like I said, not the most elegant solution ever, but it works. The script could obviously be tweaked to check to see if your actual connect is up prior to trying to reconnect the VPN connection.
kimboinatl
Level 1
Level 1
 
Posts: 14
Joined: Sun Jan 13, 2013 11:50 am

Re: VPN Auto Reconnect

Postby kimboinatl on Sun Jan 13, 2013 5:42 pm

Forgot to mention one other thing - you need to change your configuration so that you can connect/disconnect from the command line, otherwise you'll get an error. Just follow this guide:

http://www.ubuntugeek.com/ubuntu-tiphow ... -line.html
kimboinatl
Level 1
Level 1
 
Posts: 14
Joined: Sun Jan 13, 2013 11:50 am

Re: VPN Auto Reconnect

Postby Supraz on Wed Mar 13, 2013 12:12 am

kimboinatl wrote:Hey guys,

I realize that this is probably not the most elegant solution, but here's what I did.

I created this script to check to see if my VPN connection was up every 30 seconds. If it isn't it restarts it. Replace "Private Internet Access" with whatever your VPN connect is called:

Code: Select all
#!/bin/bash

while [ "true" ]
do
   VPNCON=$(nmcli con status)
   if ! grep -q "Private Internet Access" <<<$VPNCON; then
      echo "`date` Disconnected, trying to reconnect..."
      sleep 1
      nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
   fi
   sleep 30
done


I added this to my /etc/rc.local file so that it starts at bootup:

Code: Select all
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#VPN reconnect script, to ensure VPN is always on
nohup /root/scripts/reconnectvpn.sh > /var/tmp/reconnectvpn.log 2>&1 &

exit 0


Like I said, not the most elegant solution ever, but it works. The script could obviously be tweaked to check to see if your actual connect is up prior to trying to reconnect the VPN connection.



Theres a typo in that first part it should actually be

nmcli con up id Private\ Internet\ Access
not
nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad

since we were trying to avoid UUID's that could change. Works great though. Thanks for everyones input.
Supraz
Level 1
Level 1
 
Posts: 1
Joined: Wed Mar 13, 2013 12:08 am

Re: VPN Auto Reconnect

Postby MrsAngelD on Tue May 14, 2013 10:23 pm

Supraz wrote:
kimboinatl wrote:Hey guys,

I realize that this is probably not the most elegant solution, but here's what I did.

I created this script to check to see if my VPN connection was up every 30 seconds. If it isn't it restarts it. Replace "Private Internet Access" with whatever your VPN connect is called:

Code: Select all
#!/bin/bash

while [ "true" ]
do
   VPNCON=$(nmcli con status)
   if ! grep -q "Private Internet Access" <<<$VPNCON; then
      echo "`date` Disconnected, trying to reconnect..."
      sleep 1
      nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
   fi
   sleep 30
done


I added this to my /etc/rc.local file so that it starts at bootup:

Code: Select all
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#VPN reconnect script, to ensure VPN is always on
nohup /root/scripts/reconnectvpn.sh > /var/tmp/reconnectvpn.log 2>&1 &

exit 0


Like I said, not the most elegant solution ever, but it works. The script could obviously be tweaked to check to see if your actual connect is up prior to trying to reconnect the VPN connection.



Theres a typo in that first part it should actually be

nmcli con up id Private\ Internet\ Access
not
nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad

since we were trying to avoid UUID's that could change. Works great though. Thanks for everyones input.


Hi, this is exactly what I'm looking for but I'm a bit of a newbie, so is there anyway you could break this down in newbie terms for me, pretty please.

My VPN connection is called PIA - UK London

Thanks so much in advance for any help you can provide.
MrsAngelD
Level 1
Level 1
 
Posts: 5
Joined: Sun Jan 13, 2013 2:28 pm

Re: VPN Auto Reconnect

Postby rsmithee on Mon Dec 16, 2013 1:11 pm

Hallo. I know this is an older post but in case someone finds it I thought I'd mention this script that I wrote for some of these issues called ipcheck.sh. It was written for a CentOS system but it wouldn't take much to run it on most distros.

Change the XX to tt for the link, I don't like hotlinking posts since it can be construed as spam.

hXXp://code.google.com/p/ipcheck/source ... ipcheck.sh
rsmithee
Level 1
Level 1
 
Posts: 1
Joined: Mon Dec 16, 2013 1:03 pm

Re: VPN Auto Reconnect

Postby aprigoat on Wed Dec 18, 2013 7:28 pm

rsmithee hello, hello to all. Your script is useful, thank you. Tried with my Arch Linux - no problems. BTW Gentoo responds to this script? I have 2 distribution.
Have a proposal. Guys from faceless me (put dot in middle) are well aware of this topic. If you would be interested. Lots of useful information.
aprigoat
Level 1
Level 1
 
Posts: 1
Joined: Wed Dec 18, 2013 7:12 pm

Linux Mint is funded by ads and donations.
 

Return to Other networking topics

Who is online

Users browsing this forum: No registered users and 1 guest