obsd-fastest-mirror.sh (976B)
1 #!/bin/sh 2 ping_timeout=1 3 test_time=3 4 tmpd=/tmp/mirrors-speed 5 urls=$(ftp -V -o - https://www.openbsd.org/ftp.html|grep -Eo '((http|https)://.*/OpenBSD/)'|uniq) 6 m_num=$(echo "$urls"|wc -l) 7 rm -rf $tmpd 8 mkdir $tmpd 9 _table="Mirror|Ping|MB/s" 10 _i=1 11 for url in $urls 12 do 13 printf "Testing [%3d/%3d] %s" $((_i++)) "$m_num" "$url" 14 _url="${url}snapshots/ports.tar.gz" 15 _host="$(echo "$url" | sed -ne 's|.*://\([^/]*\)/.*|\1|p')" 16 _tmpf=$tmpd/$_host 17 _ping=$(ping -w $ping_timeout -c 1 "$_host" 2>/dev/null | sed -nE 's/.*time=([0-9.]+).*/\1/p') 18 [ -z "$(echo $_ping)" ] && _ping="0" 19 timeout $test_time ftp -MVo "$_tmpf" "$_url" >/dev/null 2>&1 20 [ -s "$_tmpf" ] && { 21 _size=$(stat -f%z "$_tmpf" 2>/dev/null) 22 _speed=$(echo "scale=7; $_size / $test_time / 1024 / 1024" | bc) 23 } || _speed="0" 24 _row="$(printf "%s|%.3f|%f" "$url" "$_ping" "$_speed")" 25 _table="${_table}\n${_row}\n" 26 rm -f "$_tmpf" 27 printf "\033[2K\r" 28 done 29 echo "$_table" | sort -t'|' -n -k3 | column -t -s'|' 30 rm -rf $tmpd