I don't know if my first email got through (I never got a reply), but attached is my final product. Takes the "base connection id" and "vpn id" as arguments. On Fri, 2011-12-16 at 23:52 -0800, chris wrote: > I am writing a (python) script to restart VPN connections if they shut > down. Here's what I have: > > #!/usr/bin/env python > > import gobject, dbus > from dbus.mainloop.glib import DBusGMainLoop > > CONNECTED = 5 > DISCONNECTED = 7 > > DBusGMainLoop(set_as_default=True) > system_bus = dbus.SystemBus() > > def vpn_connection_handler(*args, **kwargs): > > state = args[0] > if state == CONNECTED: > print "connected" > elif state == DISCONNECTED: > print "disconnected" > ppp0 = system_bus.get_object('org.freedesktop.NetworkManager', > '/org/freedesktop/NetworkManager/VPN/Plugin') > ppp0_dev_iface = dbus.Interface(ppp0, > dbus_interface='org.freedesktop.NetworkManager.VPN.Plugin') > value = [] > props = ppp0_dev_iface.Connect({"ppp0": {"ppp0": value}}) > print props > > system_bus.add_signal_receiver(vpn_connection_handler, > dbus_interface="org.freedesktop.NetworkManager.VPN.Connection", > signal_name="VpnStateChanged") > gobject.MainLoop().run() > > > > If you try it, you'll notice that the reconnection part doesn't work. > Little help? > > Thanks!
#!/usr/bin/env python import logging import sys, getopt import time import gobject, dbus from dbus.mainloop.glib import DBusGMainLoop NM_DEVICE_STATE_ACTIVATED = 100 CONNECTED = 5 FAILED = 6 DISCONNECTED = 7 interfaceQuery = None vpnQuery = None DBusGMainLoop(set_as_default=True) class DbusHelper: basePackage = None systemBus = dbus.SystemBus() def __init__(self, basePackage): self.basePackage = basePackage def createProxy(self, path): return self.systemBus.get_object(self.basePackage, path) def createInterface(self, proxy, interfacePackage): return dbus.Interface(proxy, dbus_interface=interfacePackage) def createIface(self, proxyPath, interfacePackage): return self.createInterface(self.createProxy(proxyPath), interfacePackage) def addReceiver(self, handler, interface, signalName): self.systemBus.add_signal_receiver(handler, dbus_interface=interface, signal_name=signalName) class DbusPropertiesHelper: interfacePath = None propertiesI = None def __init__(self, proxyPath, dbusHelper, interfacePath): self.interfacePath = interfacePath self.propertiesI = dbusHelper.createIface(proxyPath, 'org.freedesktop.DBus.Properties') def get(self, prop): return self.propertiesI.Get(self.interfacePath, prop) class NetworkManagerHelper: dbusHelper = DbusHelper('org.freedesktop.NetworkManager') networkManagerI = dbusHelper.createIface('/org/freedesktop/NetworkManager', 'org.freedesktop.NetworkManager') settingsI = dbusHelper.createIface('/org/freedesktop/NetworkManager/Settings', 'org.freedesktop.NetworkManager.Settings') def findConnection(self, query): connection = None for c in self.getConnections(): ci = self.dbusHelper.createIface(c, 'org.freedesktop.NetworkManager.Settings.Connection') settings = ci.GetSettings() if settings['connection']['id'].find(query) != -1: connection = c break return connection def getConnections(self): return self.settingsI.ListConnections() def getConnectionPath(self, uuid): connection = None for c in self.getConnections(): settings = settingsI.GetSettings() if settings['connection']['uuid'] == uuid: connection = c break return connection def getActiveConnection(self, path): connection = None nmdph = DbusPropertiesHelper('/org/freedesktop/NetworkManager', self.dbusHelper, 'org.freedesktop.NetworkManager') for a in nmdph.get('ActiveConnections'): acdph = DbusPropertiesHelper(a, self.dbusHelper, 'org.freedesktop.NetworkManager.Connection.Active') if acdph.get('Connection') == path: connection = a break return connection def activateVpn(self, connection, specific_object): self.networkManagerI.ActivateConnection(connection, "/", specific_object) def isConnectionActive(self, connectionPath): c = self.getActiveConnection(connectionPath) return c != None def activateVpnIf(self, interfaceQuery, vpnQuery): baseConnectionPath = self.findConnection(interfaceQuery) baseConnected = self.isConnectionActive(baseConnectionPath) while (baseConnected is False): logging.debug("waiting for base connection") time.sleep(10) baseConnected = self.isConnectionActive(baseConnectionPath) baseActiveConnection = self.getActiveConnection(baseConnectionPath) vpnConnectionPath = self.findConnection(vpnQuery) vpnConnected = self.isConnectionActive(vpnConnectionPath) if (vpnConnected is False): self.activateVpn(vpnConnectionPath, baseActiveConnection) connected = self.isConnectionActive(vpnConnectionPath) def loop(self): self.dbusHelper.addReceiver(vpnConnectionHandler, "org.freedesktop.NetworkManager.VPN.Connection", "VpnStateChanged") gobject.MainLoop().run() def vpnConnectionHandler(*args, **kwargs): state = args[0] logging.debug("state %s", state) if ((state == FAILED) or (state == DISCONNECTED)): networkManager.activateVpnIf(interfaceQuery, vpnQuery) if __name__ == "__main__": logging.basicConfig(level=logging.ERROR) interfaceQuery = sys.argv[1] vpnQuery = sys.argv[2] networkManager = NetworkManagerHelper() networkManager.activateVpnIf(interfaceQuery, vpnQuery) networkManager.loop() sys.exit(1)
Attachment:
signature.asc
Description: This is a digitally signed message part