Skip to content

Instantly share code, notes, and snippets.

@antoniy antoniy/vpn-auto-reconnect.sh Secret
Last active May 26, 2019

Embed
What would you like to do?
Auto reconnect to OpenVPN via NetworkManager
#!/bin/bash +x
# Source: http://www.gabsoftware.com/tips/automatically-reconnect-to-your-vpn-on-linux/
# Description:
# Make the script executable "chmod +x /path/to/the/script.sh
# Put the script in .profile or .bashrc so it can be run on user login:
# Example: echo "/path/to/the/script.sh start &" >> .bashrc
# The script can be bound to shortcut keys with these commands:
# /path/to/the/script.sh start # starts and monitors VPN connection
# /path/to/the/script.sh stop # stops the monitor and also the VPN connection
##########
# Config #
##########
# You can see those with "nmcli con" command
VPN_NAME="VPN-NAME-HERE"
VPN_UID="VPN-UID-HERE"
# Delay in secconds
DELAY=30
# File path with write permission to the executing user to store script status information
LOG="/path/to/log/file.log"
# Enable/disable ping connection check
PING_CHECK_ENABLED=true
# Check IP/Hostname
CHECK_HOST="8.8.8.8"
# Configure DISPLAY variable for desktop notifications
DISPLAY=0.0
##################
# Implementation #
##################
if [[ $1 == "stop" ]]; then
nmcli con down uuid $VPN_UID
echo "VPN monitoring service STOPPED!"
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> VPN monitoring service STOPPED!" >> $LOG
notify-send "VPN monitoring service STOPPED!"
SCRIPT_FILE_NAME=`basename $0`
PID=`pgrep -f $SCRIPT_FILE_NAME`
kill $PID
elif [[ $1 == "start" ]]; then
while [ "true" ]
do
VPNCON=$(nmcli con status | grep $VPN_NAME | cut -f1 -d " ")
if [[ $VPNCON != $VPN_NAME ]]; then
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> Disconnected from $VPN_NAME, trying to reconnect..." >> $LOG
(sleep 1s && nmcli con up uuid $VPN_UID)
else
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> Already connected to $VPN_NAME!" >> $LOG
fi
sleep $DELAY
if [[ $PING_CHECK_ENABLED = true ]]; then
PINGCON=$(ping $CHECK_HOST -c2 -q -W 3 |grep "2 received")
if [[ $PINGCON != *2*received* ]]; then
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> Ping check timeout ($CHECK_HOST), trying to reconnect..." >> $LOG
(nmcli con down uuid $VPN_UID)
(sleep 1s && nmcli con up uuid $VPN_UID)
else
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> Ping check ($CHECK_HOST) - OK!" >> $LOG
fi
fi
done
echo "VPN monitoring service STARTED!"
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> VPN monitoring service STARTED!" >> $LOG
notify-send "VPN monitoring service STARTED!"
else
echo "$(date +%Y/%m/%d\ %H:%M:%S) -> Unrecognised command: $0 $@" >> $LOG
echo "Please use $0 [start|stop]"
notify-send "UNRECOGNIZED COMMAND" "VPN monitoring service could not recognise the command!"
fi
@zijlstra-it

This comment has been minimized.

Copy link

zijlstra-it commented Nov 5, 2014

in stead of using a log file, just use the "logger" command to send it to syslog.... that's how I implemented this little script of yours.... never knew the "notify-send" utility though ;-)

@joejacobs

This comment has been minimized.

Copy link

joejacobs commented Jul 22, 2015

Not sure why but my version of nmcli (0.9.10.0) does not recognise the command nmcli con status. Replacing it with nmcli con show --active seems to work.

@GusBricker

This comment has been minimized.

Copy link

GusBricker commented Aug 24, 2015

You don't need the uuid, you can just use the id (the connection name shown in nmi con list).

@lmjh

This comment has been minimized.

Copy link

lmjh commented Sep 9, 2015

This script was failing to connect for me on Mint 17.2 and returning a "no valid VPN secrets" error. If anyone else has this problem, I resolved it by following the instructions in this article to edit the VPN connection settings: http://www.ubuntugeek.com/ubuntu-tiphow-to-connectdisconnect-vpn-from-the-command-line.html

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 28, 2016

Is it possible to insert a second VPN server option after line 56, such as having VPN_UID2 and VPN_NAME2 to connect to if VPN_UID no longer connects because of downtime? otherwise it will try reconnect to the same vpn infinitely. It should say that -
if VPN_UID Disconnected from $VPN_NAME, trying to reconnect..." message repeated 3 times, then
(sleep 1s && nmcli con up uuid $VPN_UID2)
How to do this? im not that good with coding. thanks in advance

@arcooke

This comment has been minimized.

Copy link

arcooke commented Feb 22, 2017

Thanks everyone for leaving solutions to your problems. @joejacobs and @lmjh I ran into both of those issues on Debian Jessie

@whattheserver

This comment has been minimized.

Copy link

whattheserver commented Apr 15, 2019

very nice script working nicely with the suggestion by @joejacobs for Mint 19
find "nmcli con status" replace "nmcli con show --active"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.