I proposed to implement this feature in http://brainstorm.ubuntu.com/idea/27164/, but it was marked as already implemented. Could someone shed light on how i can do it? I would like a shortcut to a specific connection (vpn or broadband) on my panel (one click connect).
You should be able to create shortcuts that call nmcli to bring up connections:
nmcli con up id "name of your connection"
Here's the result when I run this in a terminal on my system:
% nmcli con up id "PPTP VPN Testing"
Active connection state: activating
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/2
state: VPN connecting (3)
state: VPN connecting (getting IP configuration) (4)
Connection activated
NetworkManager has D-Bus interface, which you can use in other programs or scripts.
For example, I managed to connect one of my network-manager's VPN connections using this command:
qdbus --system \
org.freedesktop.NetworkManager \
/org/freedesktop/NetworkManager \
org.freedesktop.NetworkManager.ActivateConnection \ #-- this is a method
org.freedesktop.NetworkManagerUserSettings \ #-- arg 1
/org/freedesktop/NetworkManagerSettings/4 \ #-- arg 2
/org/freedesktop/NetworkManager/Devices/1 \ #-- arg 3
/ #-- dumb arg 4 (needed for wifi only)
You can browse your D-Bus infrastructure with qdbusviewer
tool from the qt4-dev-tools
package. CLI-only qdbus
is from libqt4-dbus
.
NetworkManager's D-Bus API is documented here.
When you find your needed parameters (connection number, device number, etc), you could save the lines as a shell script and create a launcher on the panel to quickly access it.
Personally I don't bother and just do the two additinal clicks. After all, I'm not connecting to VPN too often to suffer from them.
I use cnetworkmanager with this python script for convenience. The script first starts LAN connection, then opens VPN.
#!/usr/bin/env python
import subprocess
import time
import sys
def getConnectionState():
"""Returns 'CONNECTED' or 'DISCONNECTED'
"""
p = subprocess.Popen("cnetworkmanager --state", shell=True, stdout=subprocess.PIPE)
stdout = p.communicate()
result = stdout[0].split('\n')[0]
return result
def isConnected():
return getConnectionState() == 'CONNECTED'
def connectEthernet(name='myethernet'):
p = subprocess.Popen("cnetworkmanager --activate-connection=user,%s,," % name, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def connectVPN(name='myvpn'):
p = subprocess.Popen("cnetworkmanager --activate-connection=user,%s,eth0," % name, shell=True)
def timeout():
while True:
time.sleep(0.2)
if isConnected():
break
connectEthernet()
timeout()
connectVPN(sys.argv[1])