Skip to content

Instantly share code, notes, and snippets.

@asowder3943
Last active August 19, 2022 01:39
Show Gist options
  • Save asowder3943/b6a0bace590058d3be108f2f96fa9e98 to your computer and use it in GitHub Desktop.
Save asowder3943/b6a0bace590058d3be108f2f96fa9e98 to your computer and use it in GitHub Desktop.
ExpressVPN - Connect to Random VPN Server
#!/bin/bash
#
# Express VPN Connect to Random Location
# *** Note: It is highly recommended the client be configured to block traffic on vpn disconnect
#
#
# Options - accept vpn cli as argument
# Currently Supported CLIs
# expresso (MAC OS): https://github.com/sttz/expresso
# expressvpn (Linux): https://www.expressvpn.com/support/vpn-setup/app-for-linux/#download
#
while getopts v: flag
do
case "${flag}" in
v) VPN=${OPTARG};;
esac
done
echo "VPN-CLI: $VPN";
#
# Disconnect from current VPN Node
# It is recommended that smart lock is enabled before using this script to avoid leaking traffic
# Sleep for 1 second following disconnection to avoid potential race conditions
# *** Note: It is highly recommended the client be configured to block traffic on vpn disconnect
#
case $VPN in
expresso)
expresso disconnect >/dev/null 2>&1
;;
expressvpn)
expressvpn disconnect >/dev/null 2>&1
;;
esac
sleep 1
echo "[$(date +'%H:%M:%S')] VPN Disconnected."
#
# Select a new VPN location
# Parses output form the 'list' or 'locations' command and chooses a random server
#
case $VPN in
expresso)
VPN_LOCATION=$(expresso locations | grep -o '([0-9])\|([0-9][0-9])\|([0-9][0-9][0-9])' | tr -d '()' | gshuf | head -n 1)
;;
expressvpn)
VPN_LOCATION=$(expressvpn list | awk -F" " '{ print $1 }' | tail -n +4 | head -n -3 | shuf | head -n 1)
echo $(expressvpn list | awk -F" " '{ print $1 }')
;;
esac
echo "[$(date +'%H:%M:%S')] New VPN location selected: ${VPN_LOCATION}"
#
# Connect to newly selected location
# Sleep for 1 second following connection to avoid potential traffic leakage
# *** Note: It is highly recommended the client be configured to block traffic on vpn disconnect
#
echo "[$(date +'%H:%M:%S')] Connecting to the new location."
case $VPN in
expresso)
expresso connect -c ${VPN_LOCATION}
;;
expressvpn)
expressvpn connect ${VPN_LOCATION}
;;
esac
sleep 1
echo "[$(date +'%H:%M:%S')] Connected to ${VPN_LOCATION}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment