Skip to content

Instantly share code, notes, and snippets.

@smcpeck
Created November 4, 2017 23:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save smcpeck/d89c730f7b1a0bc4acdcbf9d1a86d187 to your computer and use it in GitHub Desktop.
Save smcpeck/d89c730f7b1a0bc4acdcbf9d1a86d187 to your computer and use it in GitHub Desktop.
Shell script to select fastest VPN location from available list
#!/bin/bash
# Set your VPN up with this guide: https://gist.github.com/superjamie/ac55b6d2c080582a3e64
# This runs nicely on a Raspberry Pi that is setup to be your gateway.
# Requires: speedtest-cli, openvpn
# Assuming you have config files of:
# /etc/openvpn/newyork.conf
# /etc/openvpn/chicago.conf
# /etc/openvpn/miami.conf
# Create a text file called /etc/openvpn/list.txt that looks like:
# newyork
# chicago
# miami
# Then run:
# sudo /etc/openvpn/vpn-refresh.sh /etc/openvpn/list.txt
# The fastest location will be auto detected.
if [ "$1" = "" ];
then
echo "Usage: $0 vpn-list.txt [acceptable-speed]"
echo " vpn-list.txt: text file with list of config files setup to pass to openvpn service."
echo " acceptable-speed: (Optional) minimum speed to automatically accept and skip scanning additional VPNs."
exit 1
fi
# default speed that short circuits the process to 1gbps
ACCEPTSPEED=1000
if [ "$2" != "" ];
then
ACCEPTSPEED=$2
fi
SERVERCOUNT=`wc -l "$1" | cut -d ' ' -f 1`
SPEEDGOOD=0
SERVERNUM=0
BESTSPEED=0
BESTSERVER=0
SERVERLIST=$1
echo Found $SERVERCOUNT servers in $1.
echo
testspeed ()
{
mbitdown=`speedtest-cli | grep Download | cut -d ' ' -f 2 | cut -d '.' -f 1`
if [ "$mbitdown" -gt "$BESTSPEED" ]; then
BESTSPEED=$mbitdown
BESTSERVER=$SERVERNUM
fi
echo Download speed for $SERVER is $mbitdown.
echo
}
nextserver ()
{
if [ "$SERVERNUM" -eq "$SERVERCOUNT" ]; then
exit 0
fi
((SERVERNUM++))
SERVER=`sed -n "${SERVERNUM}p" "${SERVERLIST}"`
}
resetvpn ()
{
echo -n Flipping VPN to $SERVER
# stop existing VPN connection
service openvpn stop; sleep 1;
echo -n "."
IP=`curl -s ipinfo.io/ip`
service openvpn@$SERVER start;
NEWIP=`curl -s ipinfo.io/ip`
echo -n "."
# wait 1 second while checking if IP address has changed each time
# this makes sure we are connected to the new server before moving on
while [ "$IP" = "$NEWIP" ]
do
echo -n "."
sleep 1
NEWIP=`curl -s ipinfo.io/ip`
done
echo " FLIPPED!"
}
for i in `seq 1 ${SERVERCOUNT}`;
do
if [ $BESTSPEED -lt $ACCEPTSPEED ];
then
nextserver
resetvpn
testspeed
fi
done
if [ $BESTSPEED -lt $ACCEPTSPEED ];
then
((SERVERNUM=BESTSERVER-1))
nextserver
resetvpn
fi
@Yanik39
Copy link

Yanik39 commented Jun 1, 2020

I have 2-3 minutes between nextservers.
after disconnect from a server, service openvpn@$SERVER start; takes 2-3 minutes.
do you have any idea why?
its not related to your script, just asking in general.

@smcpeck
Copy link
Author

smcpeck commented Jun 1, 2020

@TemhAAhmet
There's a good amount of stuff that takes place when connecting to your VPN server.
The authentication, getting assigned a local IP address, setting up routes to go over the VPN, etc...

I can't say I've timed this on my setup, but it definitely takes longer than I would like and I always feel like it is "too long."

Something I don't like about this old script (I don't use it anymore) is that it connects to every single VPN server and can cause your internet connection to be a little finicky during the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment