-
-
Save pacmac/a65cc82b89caa67446b73d52eb55514a to your computer and use it in GitHub Desktop.
#!/bin/ash | |
# | |
# /usr/share/scripts/xvpnping.sh yes | |
# expressvpn fastest host script | |
# | |
# /etc/openvpn/hosts" | |
# uk-berkshire-2-ca-version-2.expressnetw.com | |
# uk-east-london-ca-version-2.expressnetw.com | |
# uk-london-ca-version-2.expressnetw.com | |
# uk-kent-ca-version-2.expressnetw.com | |
# | |
# /etc/openvpn/host" | |
# uk-berkshire-2-ca-version-2.expressnetw.com | |
# | |
# echo "config /etc/openvpn/host" >> /etc/openvpn/openvpn.conf | |
# | |
## Initial Defaults | |
Smallest=1000000 | |
Server="" | |
## Params | |
Tolerance=5 | |
Pings=4 | |
## Files | |
input="/etc/openvpn/hosts" | |
output="/etc/openvpn/host" | |
## Ping Function | |
func_ping () { | |
PingMs1=`ping -c $Pings -q -i .2 $myHost | grep avg | awk -F'/' '{print $5}' | sed -e "s/ ms//"` | |
PingMs=${PingMs1/./} | |
PingMs=$((PingMs / 1000)) | |
#echo "$myHost $PingMs" | |
} | |
## Get Current Host | |
_host=$(cat $output | tr -d "\n" | tr -d "\r") | |
host=$(echo $_host | awk '{print $2}') | |
# echo -e "CURRENT:\t$host" | |
## Get Current Host Ping | |
myHost=$host; | |
func_ping; | |
Smallest=$PingMs; | |
Server=$host; | |
echo "+++++++++++++++" | |
echo -e "CURRENT:\t$host ($Smallest)" | |
echo "+++++++++++++++" | |
## Loop Thru All Hosts | |
while IFS= read -r myHost | |
do | |
func_ping; | |
## Debugging | |
if [ ! -z $1 ]; then | |
echo -e "$PingMs\t\t$myHost" | |
fi | |
if [ "$((Smallest - PingMs))" -gt $Tolerance ];then | |
Server=$myHost | |
Smallest=$PingMs | |
fi; | |
done < "$input" | |
## STATUS | |
echo "+++++++++++++++" | |
echo -e "FASTEST:\t$Server ($Smallest)" | |
echo "+++++++++++++++" | |
HOST="remote $Server 1195" | |
#echo "$PingMs $Server" | |
## QUERY-ONLY | |
if [ ! -z $1 ]; then | |
echo "Query Only." | |
fi | |
## Do UPDATE | |
if [ "$Server" == "$host" ]; then | |
echo -e "NOCHANGE:\t$host ($Smallest)" | |
exit 0; | |
else | |
#Server="" | |
if [ -z $Server ]; then | |
echo "ERROR: Fastest Host is NOT set." | |
else | |
echo "UPDATE: $HOST" | |
echo $HOST > $output | |
/sbin/service openvpn stop; | |
sleep 2 | |
/sbin/service openvpn start; | |
fi | |
fi | |
you have to create these 2 files, hosts contains a list of servers (copied from the openvpn config for each expressvpn server) and host contains a single line "host" that is included in your openvpn config file running on the host the script executes on.
I have updated the head of the script to show the files to be added and the line to be included in the /etc/openvpn/openvpn.conf file.
I think ping
command is not properly configured, i get error
I have manjaro OS
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
+++++++++++++++
CURRENT: (0)
+++++++++++++++
+++++++++++++++
FASTEST: (0)
+++++++++++++++
NOCHANGE: (0)
cat: /etc/openvpn/host: No such file or directory
How this scripts get expressvpn servers?