Automate ExpressVPN Connection Switching on Linux | Ubuntu ExpressVPN is undoubtedly one of the best VPN clients out there. They have versions of their VPN software for every single operating system and all of them are actual applications which help real noobies to connect to a VPN with the greatest of ease. However their Linux client is a command line tool, which is actually great for Linux lovers because you can then script it.

First you need to sign up and pay for ExpressVPN and I would suggest, if you are unsure to start off on their monthly option, which is more expensive and when you are happy you can switch to their annual option at any time and save over 35%. I’ve been using it for over a year now and I switched to their annual option as their system really works and their support is excellent.

Once you are subscribed you must then install ExpressVPN on your Linux operating system and they have very straight forward and simple instructions to do this.

Now once it’s installed on your Linux operating system you need to make it connect to work and that is as simple and opening a terminal and typing the following command.

expressvpn connect

So that was easy, now you are connected to a smart location and you can confirm this by visiting this link. Ok so that’s great BUT ….. ExpressVPN has a tendency to sometimes just disconnect from the location your are connected to and you won’t know about it until you visit the link above and see you are no longer connected or you type

expressvpn status

expressvpn-disconnectedAnd you see the message “Not connected” … horror of all horrors.

Ok so now what? Houston we have an ExpressVPN problem.

expressvpn-vpn-application-operating system-support
All the Operating Systems supported by ExpressVPN

Their applications for other operating systems offer the option to kill your network connection completely should it ever disconnect and most of the apps for the other operating systems allow the app to reconnect itself.

But hey we are on Linux and we can do even better than that by scripting ExpressVPN to automatically connect to a smart location after a reboot but we also take it one step further and we make our script switch smart locations every hour of the day. So you have a VPN connection that hops around the globe every 60 minutes. And it is truly as easy as this.

First let’s script ExpressVPN to connect automatically at startup after a reboot of our operating system and to do this we use the magical CRON and most users will be running all commands using sudo. Some linux distro’s don’t use sudo but all the one’s I use do.

Open terminal and type.

sudo crontab -e

This will open the CRON list where you can set script to run at certain intervals. In this case we want this event to happen on reboot. So for most people your default CRON file will look like this

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

So first of all you can remove all that commenting in the file so we have a nice clean CRON file that looks like this.

# MY CRON FILE
MAILTO=""
@reboot expressvpn connect

Now save the file by pushing CTRL+X and entering Y and ENTER (this is for people using Nano as their text editor).

Ok so now reboot your operating system and after reboot open terminal and type.

expressvpn status

And you should see a message saying something like “Connected to Switzerland” or “Connected to Amsterdam“.

So that’s great we have ExpressVPN automatically connecting by itself on startup. But now we make true magic happen by making sure we are always connected and always switching locations automatically.

We now make a simple bash script to do this for us.

So in terminal again type the following

sudo nano /usr/sbin/smartexpressvpn.sh

This opens a blank file into which you will now past the contents below.

#!/bin/bash
expressvpn disconnect
expressvpn refresh
expressvpn list | tail -n+3 | column -ts $'t' | sed 's/   */:/g' > data.tmp
cat data.tmp | while read LINE
do
        if [ `echo $LINE | tail -c 2` == 'Y' ]; then
                TEMP=`echo $LINE | grep : | awk -F: '{print $1}'`
                echo "$TEMP" >> array.tmp
        fi
done
ARRAY_SIZE=`wc -l array.tmp | awk {'print $1'}`
RND_VPN=`shuf -i 1-$ARRAY_SIZE -n 1`
VPN=`sed "${RND_VPN}q;d" array.tmp`
rm -f data.tmp
rm -f array.tmp
expressvpn connect $VPN

Now we save the file by clicking CTRL+X entering Y and pushing ENTER. Ok so that was easy but now we need to make this bash script file executable and we do that by running the following commands from terminal.

sudo chmod +x /usr/sbin/smartexpressvpn.sh

Now if you want to test the script you simply run the following from the command line.

/usr/sbin/smartexpressvpn.sh

And now check where you are connected to by typing.

expressvpn status

COOL !!! now let us automate this to run every hour of the day and we once again do this by using CRON to do it for us. So type the following in terminal to get back into your CRON job list.

sudo crontab -e

and now modify your CRON job list so it looks as follows.

# MY CRON FILE
MAILTO=""
@reboot expressvpn connect
0 */1 * * * /usr/sbin/smartexpressvpn.sh

Once again we save the file by clicking CTRL+X entering Y and pushing ENTER. That’s it, every hour of the day now CRON will automatically disconnect your current VPN connection and connect to a random new Smart Location. And you can confirm this by checking where it is connected to after an hour by typing.

expressvpn status

How cool is that? And let’s say you wanted to do it every 30 minutes? Well it’s easy as changing your CRON job to look like this.

# MY CRON FILE
MAILTO=""
@reboot expressvpn connect
*/30 * * * * /usr/sbin/smartexpressvpn.sh

That’s it …. now you have a truly SMART ExpressVPN Connection and will always be connected and always hopping the globe. ENJOY !!!

4 thoughts on “ExpressVPN Automated Connection Switching on Linux | Ubuntu

  1. Fabien Roussel says:

    Thanks for the tips.

    A shorter version of the script could be :

    expressvpn disconnect
    expressvpn refresh
    VPN=$(expressvpn list | grep ‘\Y$’ | awk ‘{print $1;}’ | shuf -n 1)
    expressvpn connect $VPN

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.