-
-
Save antoniy/f925ae55410a092c9e75 to your computer and use it in GitHub Desktop.
#!/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 |
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
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
Thanks everyone for leaving solutions to your problems. @joejacobs and @lmjh I ran into both of those issues on Debian Jessie
very nice script working nicely with the suggestion by @joejacobs for Mint 19
find "nmcli con status" replace "nmcli con show --active"
Hope this thread is still live!
I've been trying to make this script work but so far have not been successful. I keep getting the following error:
"Unrecognized Command: VPN monitoring service could not recognize the command!"
I did try to replace "nmcli con status" with "nmcli con show --active".
My OS is Linux Mint 19 Cinnamon (Cinnamon version 3.8.9)
Thanks in advance for any advice!
Edit: Never mind. It turned out the script doesn't work with .bashrc for my system. .Profile worked.
You don't need the uuid, you can just use the id (the connection name shown in nmi con list).