BASH Scripting for the Beginner

As part of our BASH Scripting discussion, here is a script I made several years ago. The script watches your VPN connection, and if it can’t reach a given address ofr a ping, will pause your program using the VPN, kill the VPN, and restart it, then when the ping is good again, un-pause the program you are running via VPN.

I’ve seen much improvememnt in VPN connectivity, and remaining connected with the most recent versions of Ubuntu, but when I work from home I would connect to my Work VPN, as per the company policy. One problem I found was that my VPN would drop without notice, or simply freeze and no longer allow network traffic, so this little script helped automate keeping me up and running with minimal interruption.

In this case I’ve replace my work program with ‘transmission-daemon’. I use Torrents to download any .iso file of a linux distort I can. I find that some ISPs like to throttle or block your bandwidth when using torrents, even when it’s for completely legal purposes, so again a VPN is a great way to bypass their incessant need to control your internet usage.

#!/bin/bash
#This program automatically re-establishes your VPN connection and pauses a predefined program.

#Monitor the VPN connection
date
while true; do
    #Define a string to test VPN connection: 0 = no connection, 1 = connected.
    programID=$(pidof transmission-daemon) # run top to identify the program you want to pause as needed
    connected=$(nmcli -t -f VPN con status|grep -c yes)	# checks to see if the VPN is connected.  VPN is identified in several ways, so you have to figure out which is the best in your system.
    case $connected in
    "0")
	    Now=$(date)
	    echo "$Now"
	    echo "VPN disconnected."

	    #Connection lost. Stop the VPN dependency, if running.
	    if [ "$programID" != "" ]; then
		    kill -SIGSTOP $programID
		    echo "Program stopped."
	    else
		    echo "Program not running."
	    fi
	    echo "Giving VPN time to completely stop."
	    echo "-----------------------------------------"
	    #Give the VPN time to recover.
		    sleep 15
		    echo "Reconnecting VPN."
	    #Retry the VPN
		    nmcli -p con up uuid 34593c15-1180-423e-b67a-a847019b3a48 # this is the UUID of the VPN you want to use.
    ;; # end of the 1st segment of the case statement
    "1")
        date
	    echo "VPN connected."
	
	    if ! [ "`ping -c 1 107.170.55.189`" ]; then #if ping exits nonzero...
            	    echo "Ping is Dead - Need to Restart VPN" #run the first script
			    #Stop the Program - Kill VPN - Restart VPN
				    if [ "$programID" != "" ]; then
					    kill -SIGSTOP $programID
					    echo "Program stopped."
				    else
					    echo "Program not running."
				    fi
				    echo "Killing VPN"
			    #Kill the VPN
				    nmcli -p con down uuid 34593c15-1180-423e-b67a-a847019b3a48
				    sleep 12
				    echo "Restarting VPN"
			    #Retry the VPN
				    nmcli -p con up uuid 34593c15-1180-423e-b67a-a847019b3a48

            	    sleep 15     #give it a few seconds to complete
	    else
		    echo "Ping is Good!"
        fi

	    #Connection is fine. Restart the VPN dependency, if it was running.
	    if [ "$programID" != "" ]; then
		    kill -SIGCONT $programID
		    echo "Program running."
		    echo "-----------------------------"
	    else
		    echo "Program not running."
	    fi
    ;;
    esac
sleep 12
done