LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2012, 02:17 AM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 31
Posts: 225

Rep: Reputation: 29
Changing VPN auto-connect options in python - code issues!


Hi,

I've looked up python conditional expressions, and tried to implement some, but just haven't been able to make any progress. I guess it doesn't help that I've never touched anything python before. So, I was hoping I could get some help on this!

I found a great script to auto-connect to a specified VPN anytime my Wifi is enabled and the particular Wifi network specified in the script is connected to. I'm trying to put a slight modification into this VPN auto-connect script so that when a Wifi network *other than the one specified* is connected to, the VPN auto-connects. I've tried throwing in the != instead of ==, but to be totally honest, I have no idea what it does on an actual level, which isn't good. And, in any case, it didn't work.

Below is the script. I'd greatly appreciate any help or pointers with this. Thanks!!

Code:
#!/usr/bin/python2

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

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

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I configure the VPN to auto connect in Fedora ? Also Ipv6 related question TK_AK Linux - Networking 2 12-16-2010 06:01 PM
Adding custom info to TCP Header Options by changing Kernel Source code abhishek@LQ Linux - Networking 0 08-26-2010 11:29 PM
PPTP VPN connect automatically (auto dial home) exactiv Linux - Networking 1 11-26-2008 06:20 PM
Linux VPN automatic connect options AlteRFirE Linux - Software 0 04-17-2008 01:43 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration