Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Last active January 13, 2016 09:39
Show Gist options
  • Save hongkongkiwi/ff8b48b10ae8069278de to your computer and use it in GitHub Desktop.
Save hongkongkiwi/ff8b48b10ae8069278de to your computer and use it in GitHub Desktop.
Bash script to test a list of Astrill Servers and returns the one with the best ping. Currently only works for the TCP servers (I am open to a way to test UDP servers). Only tested on OSX, should work fine on linux too.
#!/bin/bash
# INSTRUCTIONS
# After generating the OpenVPN server certificates, download and unzip then run this script in the same directory.
# Don't forget to chmod +x fastest-server.sh after downloading
#
# To test all servers with UK in the name
# sudo ./fastest-server.sh UK
# To test all servers with USA in the name
# sudo ./fastest-server.sh USA
#
PING_TIMES=1
OUTPUT_FILE="results.csv"
SEARCH_PATTERN=$1
command -v hping >/dev/null 2>&1 || { echo >&2 "I require hping but it's not installed.\nYou can install it with brew install hping"; exit 1; }
if [[ $UID != 0 ]]; then
echo "Please run this script with sudo:"
echo "sudo $0 $*"
exit 1
fi
if [[ $SEARCH_PATTERN == "" ]]; then
echo "Please run this script with a search pattern:"
echo "$0 <search_pattern>"
exit 1
fi
RESULTS=()
PATTERN="*${SEARCH_PATTERN}*.ovpn"
FILES=`find . -name "$PATTERN" -type f`
echo $FILES
COUNT=`find . -name "$PATTERN" -type f -exec printf '.' \; | wc -c | tr -d ' '`
if [[ $COUNT -eq "0" ]]; then
echo "Couldn't find any files with $SEARCH_PATTERN in the name"
exit 1
fi
echo "Found $COUNT VPN Servers"
COUNTER=1
for i in $FILES; do
REMOTE=`cat "$i" | grep remote`
SERVER=`echo "$REMOTE" | cut -f2 -d' ' | tr -d $'\n'`
PORT=`echo "$REMOTE" | cut -f3 -d' ' | tr -d $'\r' | tr -d $'\n'`
NAME=`basename "$i" | tr -d $'\n'`
#echo "Pinging Server ${NAME} (${SERVER}:$PORT)"
#PINGTIME=`ping -c 4 $SERVER | tail -1| awk -F '/' '{print $5}'`
if [[ "$NAME" == *"TCP"* ]]; then
RESULT=`hping -c $PING_TIMES -S -p "$PORT" -q "$SERVER" 2>&1 | grep "round-trip" | tr -d ' ' | cut -f2 -d'=' | cut -f2 -d/`
else
RESULT=`hping -c $PING_TIMES --udp -p "$PORT" -q "$SERVER" 2>&1 | grep "round-trip" | tr -d ' ' | cut -f2 -d'=' | cut -f2 -d/`
fi
greater=`echo $RESULT'>'0 | bc -l`
if [ $greater == 1 ]; then
RESULTS+=("${NAME},${RESULT}")
fi
COUNTER=$[$COUNTER +1]
echo "$NAME $RESULT"
# if [[ $COUNTER == 5 ]]; then
# break
# fi
done
printf "%s\n" "${RESULTS[@]}" > "${SEARCH_PATTERN}_${OUTPUT_FILE}"
echo "Top 5 VPN Results"
echo "================="
sort --field-separator=',' -g -k 2,2 -k 1,1 "$OUTPUT_FILE" | awk '!/0.0/' | head -n5 | tr ',' ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment