#!/bin/sh sz=1 dltimeout=30 baselineurl="https://cdimage.debian.org/debian-cd/current/s390x/iso-cd/debian-13.0.0-s390x-netinst.iso" dl() { url=$1 dlsz=$(($2 * 1000 * 1000)) # megabytes /usr/bin/time -o .timetmpfile curl \ --silent \ --no-progress-meter \ --location \ --insecure \ --connect-timeout $dltimeout \ --range 0-${dlsz} \ ${url} -o /dev/null if [ $? -eq 0 ] then cat .timetmpfile | awk -vsz=${2} '{ print sz, "MB", $1, "s", sz/$1, "MB/s" }' else cat .timetmpfile | awk -vsz=${2} '{ print sz, "MB", 0.00, "s", 0.00, "MB/s" }' fi rm .timetmpfile } gdnsdf() { dfltip=$(host -t A pkg.freebsd.org | grep -v alias| awk '{print $4}') dflthn=$(host "$dfltip" | grep "name pointer" | awk -F. '{ print $(NF-3) }') if [ -z "$gdns" ] then gdns="$dflthn $dfltip" else gdns="$(printf "%s\n%s %s" "$gdns" "$dflthn" $dfltip)" fi } #echo "TODO: implement continuous mode" #echo "TODO: standardise output format" if ! [ -x "$(command -v curl)" ] then echo "curl not found" exit fi printf "Running on: %s\n" "$(uname)" printf "Getting FreeBSD pkg mirror list from pkg.freebsd.org " conf=$(curl --silent pkg.freebsd.org) pkgmirrors="$(printf %s "$conf" | grep freebsd.org | awk -F . '{print $2}' )" defaultmirror=$(printf %s "$pkgmirrors" | head -n 1) failedmirrors="" printf "(default \033[1;4m%s\033[0m)\n" $defaultmirror printf "Establishing baseline speed: " baseline=$(dl $baselineurl 10 | awk ' { print $5}') printf "%s MB/s\n" $baseline printf "Figuring out where you are: " testip=$(curl --silent ifconfig.net) printf "%s\n" $testip echo if [ "$1" == "default" ] then exit fi printf "targets:\t" for mirror in $pkgmirrors do if [ "$mirror" == "$defaultmirror" ] then printf "|\033[1;4m%s\033[0m" $mirror else printf "|%s" $mirror fi done printf "|\n" printf "measured:\t" measurements="" for mirror in $pkgmirrors do error=0 server=pkg0.${mirror}.freebsd.org path=FreeBSD:14:amd64/latest/packagesite.tzst url=${server}/${path} p=$(ping -c 1 ${server} | grep "time=" | \ awk -F '=' -vhost=$mirror '{print(host, "rtt", $4)}') if [ -z "$p" ] then p="$mirror rtt 9999 ms" error=1 failedmirrors="$failedmirrors $mirror" fi # Skip curl download this if the ping failed if [ $error -eq 1 ] then dl=$(echo "" | awk -vsz=${sz} '{ print sz, "MB", 0.00, "s", 0.00, "MB/s" }') else dl=$(dl $url $sz) fi m=$(printf "%s %s %s" "$(date +%s)" "$p" "$dl") if [ -z "$measurements" ] then measurements="$(printf "%s" "$m")" else measurements="$(printf "%s\n%s" "$measurements" "$m")" fi if [ $error -eq 1 ] then printf "|\033[31;1m%s\033[0m" $mirror else printf "|\033[32;1m%s\033[0m" $mirror fi gdnsdf done printf "|done\n\n" # 7 is dl time fieldll=4 # latency fielddl=10 # download time ll=$(printf "$measurements" | sort -nk $fieldll | head -n 1) fd=$(printf "$measurements" | sort -nrk $fielddl | head -n 1) printf "Lowest latency:\t\t%s\n" "$(echo $ll | awk '{print $2}')" printf "Fastest download:\t%s\n" "$(echo $fd | awk '{print $2}')" printf "\n\nBy Download Speed:\n" printf "%s\n" "$measurements" | sort -nrk $fielddl | cat -n echo "Mirrors that errored on ping: $failedmirrors" echo echo "default servers during measurment:" printf "$gdns" | sort | uniq -c # this tries to integrate the reverse dns of the address into the measurements, # but its too difficult with the new lines #for s in "$(printf "$gdns" | sort | uniq -c)" #do # echo $s # echo $(echo $s | awk '{print $2}') # h=$(host $(echo $s | awk '{print $2}') | awk -F. '{ print $7 }') # printf "%s %s\n" "$s" "$h" #done