Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I wake up my computer, the internet automatically reconnects, however the VPN does not.
Is it possible to make it automatically reconnect (using the network manager on KDE, or at least in a way that the network manager is aware if it being connected via VPN) ?

share|improve this question

1 Answer 1

I faced this issue long time back, when i had linux mint. It was very irritating . A lot of R&D led me to ubuntu forums , where i got the solution . Note that this is not my work, i am just saving you the trouble of digging through that forum .

1) Start a text editor with elevated priveleges. For example, enter at the command prompt:

    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:


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

    # The uuid of the VPN connection to activate

    # The uuid of the connection that needs to be active to start the VPN connection

    # 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"



    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]:
        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>>"
       def error_handler(*args):
       print "<<FAILURE>>"
       proxy = bus.get_object(NM_DBUS_SERVICE, NM_DBUS_PATH)
       iface = dbus.Interface(proxy, dbus_interface=NM_DBUS_INTERFACE)

        bus = dbus.SystemBus()

        #print "connections:"

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

        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."

        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."

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

        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"

        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()

3) Go to the command prompt. Run the following command:

        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:

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.

Hope this is of some help to you .

share|improve this answer
Does this help you Asaf ? If not leave a comment telling us why it does not. –  The Dark Knight Sep 29 '12 at 21:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.