Last active
September 19, 2018 14:38
-
-
Save ChadBailey/6ecc9f8013688187a1dbdb50cc847725 to your computer and use it in GitHub Desktop.
Get insight into locations and pings of pacman mirrors in Arch Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## License not yet determined... if you must share or use please give credit github.com/ChadBailey | |
#/bin/bash | |
ATTRIBUTION="This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com" | |
CITYFILE=/tmp/GeoLiteCity.dat | |
URL=http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz | |
if ! which geoiplookup >/dev/null 2>&1; then | |
echo "Error: $0 requires geoip utility." | |
exit 1 | |
fi | |
echo $ATTRIBUTION | |
MIRRORS=$(cat /etc/pacman.d/mirrorlist | grep ^Server | cut -f 2 -d "=" | cut -f 3 -d "/") | |
if ! ls $CITYFILE >/dev/null 2>&1; then | |
curl -s $URL | gunzip -c > $CITYFILE | |
fi | |
WAN_IP=$(curl -s http://whatismyip.akamai.com/) | |
WAN_GEO=$(geoiplookup -f $CITYFILE $WAN_IP | cut -f 2 -d ':' | tr -d '[:blank:]') | |
WAN_LAT=$(echo $WAN_GEO |cut -f 6 -d ",") | |
WAN_LONG=$(echo $WAN_GEO |cut -f 7 -d ",") | |
RESULT="localhost,0,0,$WAN_GEO" | |
for MIRROR in $MIRRORS; do | |
MIRROR=$(echo $MIRROR | tr -d '[:blank:]') | |
MSREPLY=$(ping -c1 -w1 $MIRROR | sed -ne '/.*time=/{;s///;s/\..*//;p;}') | |
MIRROR_GEO=$(geoiplookup -f $CITYFILE $MIRROR | cut -f 2 -d ':' | tr -d '[:blank:]') | |
MIRROR_LAT=$(echo $MIRROR_GEO | cut -f 6 -d ',') | |
MIRROR_LONG=$(echo $MIRROR_GEO | cut -f 7 -d ',') | |
MIRROR_DIST=$(echo "$WAN_LAT $WAN_LONG $MIRROR_LAT $MIRROR_LONG" | awk '{print sqrt((sqrt($1^2)-sqrt($3^2))^2)+sqrt((sqrt($2^2)-sqrt($4^2))^2); exit}') | |
RESULT="$RESULT\n$MIRROR,$MIRROR_DIST,$(echo $MSREPLY | sed 's/^$/1000+/g'),$MIRROR_GEO" | |
done | |
echo -ne $RESULT | sort -n -k1,2 -k2,3 -t ',' | column -t -s "," -N "IP/Hostname,Distance,Ping(ms),Country_Code,Region_Code,Region_Name,City_Name,Postal_Code,Lat,Long,Metro_Code,Area_Code" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment