Skip to content

Instantly share code, notes, and snippets.

Created Sep 8, 2010
Embed
What would you like to do?
#!/usr/bin/python
from dbus.mainloop.glib import DBusGMainLoop
import dbus
import gobject
import sys
##
##
# This should keep alive a VPN connection.
##
##
class AutoVPN:
def __init__(self, vpn_name, max_attempts, delay):
self.vpn_name = vpn_name
self.max_attempts = max_attempts
self.delay = delay
print "Maintaining connection for %s, reattempting up to %d times with %d ms between retries" % (vpn_name, max_attempts, delay)
self.failed_attempts = 0
self.bus = dbus.SystemBus()
self.get_network_manager().connect_to_signal("StateChanged", self.onNetworkStateChanged)
def onNetworkStateChanged(self, state):
'Handles network status changes and activates the VPN on established connection.'
print "Network state changed: %d" % state
if state == 3:
self.activate_vpn()
def onVpnStateChanged(self, state, reason):
'Handles vpn status changes and eventually reconnects the VPN.'
# vpn connected or user disconnected manually?
if state == 5 or (state == 7 and reason == 2):
print "VPN connected, or user disconnected manually"
self.failed_attempts = 0
return
# connection failed or unknown?
elif state in [6, 7]:
if self.failed_attempts < self.max_attempts:
print "Connection failed, attempting to reconnect"
self.failed_attempts += 1
gobject.timeout_add(self.delay, self.activate_vpn)
else:
print "Connection failed, exceeded %d max attempts." % self.max_attempts
self.failed_attempts = 0
def get_network_manager(self):
'Gets the network manager interface.'
print "Getting NetworkManager DBUS interface"
proxy = self.bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
return dbus.Interface(proxy, 'org.freedesktop.NetworkManager')
def get_vpn_connection(self, name):
'Gets the VPN connection interface with the specified name.'
print "Getting %s VPN connection DBUS interface" % name
proxy = self.bus.get_object('org.freedesktop.NetworkManagerUserSettings', '/org/freedesktop/NetworkManagerSettings')
iface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings')
connections = iface.ListConnections()
for connection in connections:
proxy = self.bus.get_object('org.freedesktop.NetworkManagerUserSettings', connection)
iface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings.Connection')
con_settings = iface.GetSettings()['connection']
if con_settings['type'] == 'vpn' and con_settings['id'] == name:
print "Got %s interface" % name
return connection
print "Unable to acquire %s VPN interface. Does it exist?" % name
return None
def get_active_connection(self):
'Gets the interface of the active network connection.'
print "Getting active network connection"
proxy = self.bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
iface = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties')
active_connections = iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
if len(active_connections) == 0:
print "No active connections found"
return None
print "Found %d active connection(s)" % active_connections.len
return active_connections[0]
def activate_vpn(self):
'Activates the vpn connection.'
print "Activating %s VPN connection" % self.vpn_name
vpn_con = self.get_vpn_connection(self.vpn_name)
active_con = self.get_active_connection()
if vpn_con and active_con:
new_con = self.get_network_manager().ActivateConnection('org.freedesktop.NetworkManagerUserSettings',
vpn_con,
dbus.ObjectPath("/"),
active_con)
proxy = self.bus.get_object('org.freedesktop.NetworkManager', new_con)
iface = dbus.Interface(proxy, 'org.freedesktop.NetworkManager.VPN.Connection')
iface.connect_to_signal('VpnStateChanged', self.onVpnStateChanged)
print "Connection should be active"
if __name__ == '__main__':
# check arguments
if len(sys.argv) != 2:
print 'usage: autovpn <VPN_CONNECTION_NAME>'
sys.exit(0)
# set up the main loop
DBusGMainLoop(set_as_default = True)
loop = gobject.MainLoop()
max_attempts = 5
delay = 5000
# intialize the autovpn object
AutoVPN(sys.argv[1], max_attempts, delay)
# execute the main loop
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment