20
2010
(re)connexion automatique à un VPN sous Ubuntu
En ces temps peu glorieux d’Hadopi et tous ses dérivés… un bon moyen de pouvoir surfer sans être espionné, est d’utiliser un VPN.
Pour ma part, j’ai été confronté à des problèmes de déconnexions intempestives, sans reconnexion automatique. Ce qui peut être parfois gênant…
Voilà donc un (bon) moyen de palier ce problème. Ce n’est surement pas la meilleure solution, et je ne suis pas un fou de linux.. mais au moins, elle me satisfait!
Cette solution se compose de 2 fichiers. Un “check_vpn” qui se charge de lancer le script principal à intervalles définis. Et “vpn_restart.py”, script en python qui relance la connexion si celle ci déconnectée.
check_vpn
-
-
while [ 1 ]; do
-
echo -n $(date) "## "
-
sudo /home/bsoft/Bureau/vpn_restart.py #où le script est installé
-
sleep 60 #en secondes, temps de boucle du script
-
done
vpn_restart.py
-
#http://ubuntuforums.org/showthread.php?t=1316314
-
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 = "20d3d577-51b4-435c-b408-2a3f3c8a5463"
-
-
# The uuid of the connection that needs to be active to start the VPN connection
-
ACTIVE_CONNECTION_UUID = "recupere via get_active_conn(bus)"
-
-
# Mon conn ID
-
CONN_ID = "Auto eth0"
-
# Mon conn['type']
-
CONN_TYPE = "802-3-ethernet"
-
-
# 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'])
-
-
# recupere l'uuid de la connexion active via CONN_ID et CONN_TYPE
-
def get_active_conn(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']
-
if conn['id'] == CONN_ID and conn['type'] == CONN_TYPE:
-
return conn['uuid']
-
-
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)
-
-
ACTIVE_CONNECTION_UUID = get_active_conn(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()
Le script python peut très facilement être amélioré. On peut imaginer par exemple de fermer certaines applications lorsque la connexion VPN est morte..
Pour faire fonctionner le tout, il suffit de copier le script python où vous voulez.
Puis, de copier en admin (sudo) le script check_vpn dans /etc/init.d
Il faut le rendre exécutable (chmod u+x nom_du_fichier)
Puis il faut créer un lien symbolique du script dans /etc/rc2.d afin que celui ci démarre automatique avec le système.
ln -s /etc/init.d/check_vpn /etc/rc2.d/S88check_vpn
Et voilà!
Chez moi, sous Ubuntu 9.04, ca fonctionne très bien. Il faut bien entendu au préalable avoir une connexion VPN de configurée dans le network manager.
|
Comments






