Hello,
I am running OpenVPN on Mint 12 and it works great. One annoyance though is that when I log out and log back in, it does not automatically reconnect to the VPN. Does anyone know how to resolve this?




You can easily make the network manager applet start on log-in by adding the command nm-applet to your sessions. (Under System->Preferences->Session by default) However, this doesn't mean your configured connection fires up too. To make this happen you can add another command to your session startup programs:
<blah blah blah>


gksudo gedit#!/usr/bin/python
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 = "FILL IN YOUR OWN"
# The uuid of the connection that needs to be active to start the VPN connection
ACTIVE_CONNECTION_UUID = "FILL IN YOUR OWN"
# 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'])
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)
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()nmcli con statusobjekt@linuxmint12 ~ $ nmcli con status
NAME UUID DEVICES DEFAULT VPN
Wired connection 1 e9908d28-ffec-41cd-babb-c0cefb38ae6a eth0 yes no
VPN Connection 699f17f2-0ab0-4d1d-94d3-24136ef81603 -- no yes

Note that the UUID values will be unique to your system. They shouldn't change from login to login, however, or this script wouldn't work. For your reference, here's what I see:








#!/bin/bash
while [ "true" ]
do
VPNCON=$(nmcli con status)
if ! grep -q "Private Internet Access" <<<$VPNCON; then
echo "`date` Disconnected, trying to reconnect..."
sleep 1
nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
fi
sleep 30
done
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#VPN reconnect script, to ensure VPN is always on
nohup /root/scripts/reconnectvpn.sh > /var/tmp/reconnectvpn.log 2>&1 &
exit 0


kimboinatl wrote:Hey guys,
I realize that this is probably not the most elegant solution, but here's what I did.
I created this script to check to see if my VPN connection was up every 30 seconds. If it isn't it restarts it. Replace "Private Internet Access" with whatever your VPN connect is called:
- Code: Select all
#!/bin/bash
while [ "true" ]
do
VPNCON=$(nmcli con status)
if ! grep -q "Private Internet Access" <<<$VPNCON; then
echo "`date` Disconnected, trying to reconnect..."
sleep 1
nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
fi
sleep 30
done
I added this to my /etc/rc.local file so that it starts at bootup:
- Code: Select all
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#VPN reconnect script, to ensure VPN is always on
nohup /root/scripts/reconnectvpn.sh > /var/tmp/reconnectvpn.log 2>&1 &
exit 0
Like I said, not the most elegant solution ever, but it works. The script could obviously be tweaked to check to see if your actual connect is up prior to trying to reconnect the VPN connection.

Supraz wrote:kimboinatl wrote:Hey guys,
I realize that this is probably not the most elegant solution, but here's what I did.
I created this script to check to see if my VPN connection was up every 30 seconds. If it isn't it restarts it. Replace "Private Internet Access" with whatever your VPN connect is called:
- Code: Select all
#!/bin/bash
while [ "true" ]
do
VPNCON=$(nmcli con status)
if ! grep -q "Private Internet Access" <<<$VPNCON; then
echo "`date` Disconnected, trying to reconnect..."
sleep 1
nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
fi
sleep 30
done
I added this to my /etc/rc.local file so that it starts at bootup:
- Code: Select all
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#VPN reconnect script, to ensure VPN is always on
nohup /root/scripts/reconnectvpn.sh > /var/tmp/reconnectvpn.log 2>&1 &
exit 0
Like I said, not the most elegant solution ever, but it works. The script could obviously be tweaked to check to see if your actual connect is up prior to trying to reconnect the VPN connection.
Theres a typo in that first part it should actually be
nmcli con up id Private\ Internet\ Access
not
nmcli con up uuid e724d24b-f169-40d2-87f8-a3cb2f4bf8ad
since we were trying to avoid UUID's that could change. Works great though. Thanks for everyones input.

Users browsing this forum: No registered users and 7 guests