Skip to content

Instantly share code, notes, and snippets.

@Cynnexis
Created August 28, 2020 10:45
Show Gist options
  • Select an option

  • Save Cynnexis/1b9ce548f1d74bbff9fb13d6c89de268 to your computer and use it in GitHub Desktop.

Select an option

Save Cynnexis/1b9ce548f1d74bbff9fb13d6c89de268 to your computer and use it in GitHub Desktop.
Find the best CTAN mirror to download TeX Live according to the ping.
#!/bin/bash
mirrors=$(curl -sSL http://dante.ctan.org/mirmon/ | grep -oE "<TD ALIGN=RIGHT><A HREF=\"[^\"]+" | cut -d\" -f2)
all_ctan_mirrors=($mirrors)
re_number='^[0-9.]+$'
declare -a all_ping=()
declare -a all_url=()
i=0
for ctan_mirror in ${all_ctan_mirrors[@]}; do
results=$(ping -c 1 -w 1 -W 1 $(echo $ctan_mirror | sed -re 's/^[a-zA-Z0-9]+\:\/\/([^\/]+).+$/\1/'))
avg=$(echo $results | sed -re 's/^.*=\s*[0-9.]+\/([0-9.]+).*$/\1/')
echo -n "[$(($i+1))/${#all_ctan_mirrors[@]}] $ctan_mirror: "
if ! [[ $avg =~ $re_number ]] ; then
echo "timeout"
else
echo "$avg ms"
all_ping+=($avg)
all_url+=($ctan_mirror)
fi
i=$(($i+1))
done
echo ''
echo -n "Best: "
index=$(echo "${all_ping[*]}" | tr ' ' '\n' | awk 'NR==1{min=$0}NR>1 && $1<min{min=$1;pos=NR}END{print pos}')
echo "${all_url[$index]}: ${all_ping[$index]} ms"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment