#!/usr/bin/python
#
# Original script was from 'Tambet Ingo' and can be found under
# [url]http://mail.gnome.org/archives/networkmanager-list/2009-February/msg00098.html[/url]
#
# Modified by dotzball to use it with various (VPN) connections, just call the
# script with connection names (as configured in NM) as argument:
# nm-startvpn <active connection> <vpn connection>
# e.g.
# nm-startvpn 'MyWlan' 'MyVPN'
# If the given <active connection> is connected/active, the <vpn connection> will
# be connected by this script.
# The only thing to hardcode/change in this script is the UID of the owner of the
# connection(s).
# Run this script without any arguments to list the available connection uuids.
# The uuid of the VPN connection to activate
#VPN_CONNECTION_UUID="58e0ba6b-bb82-496e-9bfa-5541ed53b01c"
VPN_CONNECTION_UUID=""
# The uuid of the connection that needs to be active to start the VPN connection
#ACTIVE_CONNECTION_UUID="00b81190-86bb-4ca6-954e-6501a94ae6c0"
ACTIVE_CONNECTION_UUID=""
# UID to use. Note that NM only allows the owner of the connection to activate it.
#UID=1000
UID=1000
import sys
import os
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
DBusGMainLoop(set_as_default=True)
def get_connections():
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', '/org/freedesktop/NetworkManagerSettings')
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings')
return iface.ListConnections()
def get_connection_by_uuid(uuid):
bus = dbus.SystemBus()
for c in get_connections():
proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', c)
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
settings = iface.GetSettings()
if settings['connection']['uuid'] == uuid:
return c
return None
def list_uuids():
bus = dbus.SystemBus()
for c in get_connections():
proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', c)
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
settings = iface.GetSettings()
conn = settings['connection']
print "%s - \'%s\' (%s)" % (conn['uuid'], conn['id'], conn['type'])
def set_uuids():
global ACTIVE_CONNECTION_UUID
global VPN_CONNECTION_UUID
bus = dbus.SystemBus()
for c in get_connections():
proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', c)
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
settings = iface.GetSettings()
conn = settings['connection']
if sys.argv[1] == conn['id']:
ACTIVE_CONNECTION_UUID = conn['uuid']
#print "Active Connection: " + ACTIVE_CONNECTION_UUID
if sys.argv[2] == conn['id']:
VPN_CONNECTION_UUID = conn['uuid']
#print "VPN Connection : " + VPN_CONNECTION_UUID
def get_active_connection_path(uuid):
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
active_connections = iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
all_connections = get_connections()
for a in active_connections:
proxy = bus.get_object('org.freedesktop.NetworkManager', a)
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
path = iface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection')
proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', path)
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
settings = iface.GetSettings()
if settings['connection']['uuid'] == uuid:
return a
return None
def activate_connection(vpn_connection, active_connection):
def reply_handler(opath):
sys.exit(0)
def error_handler(*args):
sys.exit(1)
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager')
iface.ActivateConnection('org.freedesktop.NetworkManagerUserSettings',
vpn_connection,
dbus.ObjectPath("/"),
active_connection,
reply_handler=reply_handler,
error_handler=error_handler)
# Change the UID first if required
if UID != 0:
os.setuid(UID)
#print "test 1"
# NM dispatcer always calls us with certain arguments.
# In case no arguments are provided, simply list currently known
# connections with their uuids to help with configuration
if len(sys.argv) == 1:
list_uuids()
sys.exit(0)
#print "test 2"
# get settings and find uuid of given connections
if len(sys.argv) == 3:
#print "The first argument was:", sys.argv[1]
#print "The second argument was:", sys.argv[2]
set_uuids()
#print "test 3"
# Are we configured?
if len(VPN_CONNECTION_UUID) < 1 or len(ACTIVE_CONNECTION_UUID) < 1:
sys.exit(0)
#print "test 4"
vpn_connection = get_connection_by_uuid(VPN_CONNECTION_UUID)
if not vpn_connection:
# Configured VPN connection is not known to NM, check VPN_CONNECTION_UUID.
sys.exit(1)
#print "test 5"
active_connection = get_connection_by_uuid(ACTIVE_CONNECTION_UUID)
if not active_connection:
# Configured active connection is not known to NM, check ACTIVE_CONNECTION_UUID.
sys.exit(1)
#print "test 6"
# Is it already activated?
if get_active_connection_path(VPN_CONNECTION_UUID):
sys.exit(0)
#print "test 7"
active_connection_path = get_active_connection_path(ACTIVE_CONNECTION_UUID)
if not active_connection_path:
# The required connection isn't active at the moment
sys.exit(0)
#print "test 8"
activate_connection(vpn_connection, active_connection_path)
#print "test 9"
loop = gobject.MainLoop()
#print "test 10"
loop.run()