Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Automatic VPN PPTP reconnect script

  1. #1
    Join Date
    Nov 2009
    Beans
    2

    Automatic VPN PPTP reconnect script

    I would like to share what I learnt with regards to reconnecting VPN connections. I am using 9.04 and I had the problem that my VPN connection dropped now and then, and I did not want to manually reconnect it all the time again. Obviously the Ubuntu network manager (applet) does not provide such an automatic VPN reconnect functionality, so I started searching for a solution on the internet. Several bugs have been filed with regards to that and I am surprised that nobody has taken care of this yet. This issue seems to have been ignored for quite a while.

    Nevertheless, a couple of guys tried to find a solution using scripts. I found this thread, but it did not work for me, but this one had the right solution. Thanks to Daniel Akerud who was so kind as to resend me his modified script I could work out a solution for me. It is the following python script to reconnect the VPN connection previously configured via network manager:

    First the original script from Daniel Akerud:

    nm-vpn-restart.py

    Code:
    #!/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()
    DO NOT FORGET to fill in your UUID on the top section of the script after you have run it for the first time....


    I modified it a bit for my purposes:

    nm-vpn-restart2.py

    Code:
    #!/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()
    and I created a bash script that takes care of the reconnection every 20 seconds which I run in a terminal

    Code:
    #!/bin/bash
    
    while [ 1 ]; do
       echo -n $(date) "##  "
       sudo /home/USER/Install/VPN-Control/nm-vpn-restart2.py #wherever you have installed the script
       sleep 20
    done
    I know that I will not win a programming price for that but for me it works and I am very happy with it....

  2. #2
    Join Date
    Jan 2010
    Location
    Dublin, Ireland
    Beans
    16
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Automatic VPN PPTP reconnect script

    sorry to sound a bit silly here... but what do i do with that script exactly... how do i implement it? where does it go?

  3. #3
    Join Date
    May 2007
    Beans
    29
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Automatic VPN PPTP reconnect script

    Tried this out today and it works great.

    For me the first script worked but not the second one you modified.

    What is the difference?


    Really happy for getting this to work, thank you!

  4. #4
    Join Date
    Jan 2010
    Location
    Dublin, Ireland
    Beans
    16
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Automatic VPN PPTP reconnect script

    any chance you could tell me how you done it? I couldnt open python to add that code..

  5. #5
    Join Date
    May 2007
    Beans
    29
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Automatic VPN PPTP reconnect script

    I am running Ubuntu 9.10 and python must have been installed by default. I just made the textile with the script executional and then run it in a terminal.

    The first time without any changes whatever. That gives the UUID. Then I copied thous into the script.

    That process you need to do every time you restart the computer, looks like you get new UUID every time.

    Best regards,
    /Christer

  6. #6
    Join Date
    Jan 2010
    Location
    Dublin, Ireland
    Beans
    16
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Automatic VPN PPTP reconnect script

    well that sucks

  7. #7
    Join Date
    Jul 2006
    Location
    on the bleeding edge
    Beans
    1,011

    Re: Automatic VPN PPTP reconnect script

    I think its silly that such a basic functionality is sorely missing from ubuntu for so long, with no reliable solution!

  8. #8
    Join Date
    Mar 2010
    Location
    Atlanta, Ga
    Beans
    3

    Re: Automatic VPN PPTP reconnect script

    Does the rm command work in bash scripts?

  9. #9
    Join Date
    Jul 2008
    Beans
    82
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Automatic VPN PPTP reconnect script

    Really nice script thanks for posting and sharing.

    Would be really nice to get the UUIDs somehow automagically into the script on boot. I don't boot often but still I dislike the idea of having to manually fiddle with this. I'm sure it's possible but just beyond my scripting abilities.

    couldn't we somehow grep and echo it into the script ?

  10. #10
    Join Date
    Jul 2008
    Beans
    82
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Automatic VPN PPTP reconnect script

    Nevermind my previous post, the UUIDs don't seem to change on reboot I assumed they would.

    My only problem now is that it still requires a keyring password for the vpn connection on reboot. Any way I could get rid of that keyring password asking for the VPN connection ?

    If I check the available for all users checkbox the VPN connection will no longer start.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •