UPDATE: Dorian Scholz writes in the comments; There is now a command line tool nmcli which can interact with the nm-applet and Network Manger directly. Connecting to a VPN is as easy as: nmcli con up id NAME_OF_THE_VPN
Recently I started a new job, to get my laptop on the network I first connect to their wireless LAN and then VPN into the network. Network Manager makes this fairly easily, it will automatically try and associated with the wireless network, I then have to start the VPN connection manually.
This wouldn’t be a problem if I only had to do it once or twice a day. However, wireless being wireless, drops out 4-5 times a day. While, Network Manager will automatically reassociated, I have to manually reconnect to the VPN which is a pain.
To fix this I have developed two small scripts, the first is a small shell script which will automatically call a command when connecting to certain wireless networks (/etc/NetworkManager/dispatcher.d/02runcmd). The second is a little Python script which will tell Network Manager to connect to a VPN from the command line (/usr/local/bin/nm-startvpn). When you combind these two, you get auto VPN connection goodness, yay!!
If people find this useful I might put together a config utility and properly package them.







{ 12 } Comments
I was pretty excited to find your blog posting – unfortunately it doesn’t work on my ubuntu feisty system. More specifically the python script causes nm-applet to crash and I get this feedback:
Starting ‘VPN_Name’ VPN
dbus.Array([dbus.String(u’VPN_Name’)], signature=dbus.Signature(‘s’))
(dbus.String(u’VPN_Name’), dbus.String(u’username’), dbus.String(u’org.freedesktop.NetworkManager.vpnc’), dbus.UInt32(1L))
None
I can’t restart the script manually afterwards. Not sure if that help to identify the problem.
I have tried the script as Robin did and I get the exact same result.
(To help Robin out: you can restart nm-applet by restarting NetworkManager and NetworkManagerDispatcher sevices if those should have bailed out as well (happende to me once while testing), in a terminal type:
ps -A |grep -i Network
# With the pid numbers you can now do:
sudo kill #PIDNRS
Do the same for nm-applet (grep -i nm)
Then restart NetworkManager and NetworkManagerDispatcher as root and start nm-applet using ctrl+F2)
I have taken a look at the source code of the networkmanager vpn dbus interface and I could not find any error’s in the code you used (then again, this was my first time I took a look at a phython script :p )
However we should get an output from the nm-applet (when running inside the console) saying it is connection to the VPN. (Don’t know the exact message, but run nm-applet inside the terminal and break the VPN cpnnection and set it up again, you’ll see what I mean)
I believe the script works for you Mithro? Are you running it as root, have got anything special configured? (eg inside your keyring) Anyhting that might explain the script to fail us?
I have been trying to autoconnect my vpn for a long time and basicly gave up because I didn’t feel like ‘hacking’ a script together, but this is reeeaaal close to a solution…
I hope I’ll hearing from you,
Mathijs
What version of Ubuntu are you using? It does not work on Feisty. It works fine for both me and another person on Gusty.
I have not tested with any other VPN connection apart from openvpn as well.
Ahh… Gutsy only. Would explain why it crashes on my Feisty install too 🙂
Might be good to update the earlier parts of the page to make this more explicit. Just out of interest, how did you find the details to write your script?
I don’t have any Feisty of Edgy installs to test with, hence why I didn’t comment initially. It seems to be a problem with either the older NetworkManager or the older python-dbus binding.
I found out what had to be done through reading the source code and much trial and error. Took me about 6 hours to figure out that tiny script!
Hi Mithro,
Thanks for the scripts I have implemented them for my home vpn connection I run over wireless. The only prob I have is that it fail to make the initial connection when you login after reboot or boot up and you get the pop up error saying it can’t establish the openvpn connection and you have to manually start it. Do you have the same problem? if not how have you got it to auto connect on login?
Nice scripts to by the way
Regards
Quail
thx for this little piece of goodness
but since i tested it an my configuration I think there is a problem in the sed command
your iwconfig $IFACE 2>&1 | grep ESSID | sed -e’s/.*ESSID:”\(.*\)”/\1/’ -e’s/ //g’
give me
802.1X”Nickname:”
so I changed it a little bit to
iwconfig $IFACE 2>&1 | grep ESSID | sed -e’s/.*ESSID:”\(.*\)”.*”.*”/\1/’ -e’s/ //g’
thx anyway
raporu
You are correct. For newer versions of ubuntu this change appears to be needed (I just recently upgraded to Hardy Heron and needed to make this change also).
I couldn’t get nm-startvpn running under opensuse 11.0. It gives me error:
Traceback (most recent call last):
File “/usr/local/bin/nm-startvpn”, line 29, in
print interface.getVPNConnections()
File “/usr/lib/python2.5/site-packages/dbus/proxies.py”, line 68, in __call__
return self._proxy_method(*args, **keywords)
File “/usr/lib/python2.5/site-packages/dbus/proxies.py”, line 140, in __call__
**keywords)
File “/usr/lib/python2.5/site-packages/dbus/connection.py”, line 607, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method “getVPNConnections” with signature “” on interface “org.freedesktop.NetworkManager.VPNConnections” doesn’t exist
However, I changed 02runcmd so that is just starts/stops openvpn and this works fine
#! /bin/sh
case “$2” in
up)
/usr/sbin/rcopenvpn start
;;
down)
/usr/sbin/rcopenvpn stop
;;
esac
exit 0
Not really hard:
http://www.linuxquestions.org/questions/linux-networking-3/openvpn-client-682343/
Since this page has a high google rank for this topic, here is a more elegant solution possible from NetworkManager 0.8.1 on:
There is now a command line tool nmcli which can interact with the nm-applet and NM directly.
So connecting to a VPN is as easy as this:
nmcli con up id NAME_OF_THE_VPN
For stock installation of Ubuntu 10.04 (Lucid Lynx) users, Network Manager version 0.8.0, so nmcli is not available and since there may be others like me who like to stick to the LTS, here is a solution.
Visit http://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/python?h=NM_0_8 and download list-connections.py. Mark it as executable and run it to find the UUID of your VPN connection.
Also, download vpn.py from the same link. This script is written to connect to VPN over wifi only. So, if you want to connect to VPN over your wireless network then simply execute the script.
However, for wired a minor modification in vpn.py is required . Below is the patch for the same.
*** vpn.py 2011-12-07 02:38:06.205855632 +0530
— vpn1.py 2011-12-07 02:39:49.861855276 +0530
***************
*** 86,92 ****
return None
! def get_wifi_device_path():
bus = dbus.SystemBus()
proxy = bus.get_object(‘org.freedesktop.NetworkManager’, ‘/org/freedesktop/NetworkManager’)
iface = dbus.Interface(proxy, dbus_interface=’org.freedesktop.NetworkManager’)
— 86,92 —-
return None
! def get_device_path(reqDevType):
bus = dbus.SystemBus()
proxy = bus.get_object(‘org.freedesktop.NetworkManager’, ‘/org/freedesktop/NetworkManager’)
iface = dbus.Interface(proxy, dbus_interface=’org.freedesktop.NetworkManager’)
***************
*** 95,101 ****
proxy = bus.get_object(‘org.freedesktop.NetworkManager’, d)
iface = dbus.Interface(proxy, dbus_interface=’org.freedesktop.DBus.Properties’)
devtype = iface.Get(‘org.freedesktop.NetworkManager.Device’, ‘DeviceType’)
! if devtype == 2:
return d
return None
— 95,101 —-
proxy = bus.get_object(‘org.freedesktop.NetworkManager’, d)
iface = dbus.Interface(proxy, dbus_interface=’org.freedesktop.DBus.Properties’)
devtype = iface.Get(‘org.freedesktop.NetworkManager.Device’, ‘DeviceType’)
! if devtype == reqDevType:
return d
return None
***************
*** 135,143 ****
print “couldn’t find the connection”
sys.exit(1)
! device_path = get_wifi_device_path()
if not device_path:
! print “no wifi device found”
sys.exit(1)
# Is it already activated?
— 135,143 —-
print “couldn’t find the connection”
sys.exit(1)
! device_path = get_device_path(1) #Pass 2 for wifi
if not device_path:
! print “no wired device found”
sys.exit(1)
# Is it already activated?