diff --git a/dnstest.sh b/dnstest.sh index 23082f9..c488201 100755 --- a/dnstest.sh +++ b/dnstest.sh @@ -4,9 +4,11 @@ command -v bc > /dev/null || { echo "bc was not found. Please install bc."; exit { command -v drill > /dev/null && dig=drill; } || { command -v dig > /dev/null && dig=dig; } || { echo "dig was not found. Please install dnsutils."; exit 1; } - +# Local DNS resolvers NAMESERVERS=`cat /etc/resolv.conf | grep ^nameserver | cut -d " " -f 2 | sed 's/\(.*\)/&#&/'` +# Upstream DNS resolvers +# Non-standard ports may be specified e.g. 127.0.0.1:5353#mydns PROVIDERS=" 1.1.1.1#cloudflare 4.2.2.1#level3 @@ -22,28 +24,46 @@ PROVIDERS=" 8.26.56.26#comodo " -# Domains to test. Duplicated domains are ok -DOMAINS2TEST="www.google.com amazon.com facebook.com www.youtube.com www.reddit.com wikipedia.org twitter.com gmail.com www.google.com whatsapp.com" +# Number of domains to test +NUM_DOMAINS2TEST=10 + +# Random domains to choose from +RANDOM_DOMAINS=( +`curl -sS https://raw.githubusercontent.com/opendns/public-domain-lists/master/opendns-top-domains.txt` +`curl -sS https://raw.githubusercontent.com/opendns/public-domain-lists/master/opendns-random-domains.txt` +) +heading="DOMAINS TO TEST: "; echo -n "$heading" +results_indent=$((${#heading} - 3)) +results_tempfile=`mktemp` +domains2test="" +num_random_domains=${#RANDOM_DOMAINS[*]} -totaldomains=0 -printf "%-18s" "" -for d in $DOMAINS2TEST; do - totaldomains=$((totaldomains + 1)) - printf "%-8s" "test$totaldomains" +for ((i=1; i <= $NUM_DOMAINS2TEST; i++)); do + if [ $i -gt 1 ]; then + printf "%-${#heading}s" "" + fi + + domain_id=`printf "%5s" "($i) "`; echo -n "$domain_id" + domain_heading=" $domain_id" + results_header="$results_header$domain_heading" + random_domain=${RANDOM_DOMAINS[$RANDOM % num_random_domains]}; echo $random_domain + domains2test="$domains2test $random_domain" done -printf "%-8s" "Average" -echo "" +avg_heading=" AVERAGE" +results_header="$results_header$avg_heading" +printf "\n%-${results_indent}s" "" +echo "$results_header" for p in $NAMESERVERS $PROVIDERS; do pip=${p%%#*} pname=${p##*#} ftime=0 - printf "%-18s" "$pname" - for d in $DOMAINS2TEST; do - ttime=`$dig +tries=1 +time=2 +stats @$pip $d |grep "Query time:" | cut -d : -f 2- | cut -d " " -f 2` + printf "%-${results_indent}s" "$pname" | tee -a $results_tempfile + for d in $domains2test; do + ttime=`$dig +tries=1 +time=2 +stats @${pip/:/" -p"} $d | grep "Query time:" | cut -d : -f 2- | cut -d " " -f 2` if [ -z "$ttime" ]; then #let's have time out be 1s = 1000ms ttime=1000 @@ -51,13 +71,18 @@ for p in $NAMESERVERS $PROVIDERS; do ttime=1 fi - printf "%-8s" "$ttime ms" + printf "%${#domain_heading}s" "$ttime ms" | tee -a $results_tempfile ftime=$((ftime + ttime)) done - avg=`bc -lq <<< "scale=2; $ftime/$totaldomains"` + avg=`bc -lq <<< "scale=2; $ftime/$NUM_DOMAINS2TEST"` - echo " $avg" + printf "%${#avg_heading}s\n" $avg | tee -a $results_tempfile done +printf "\n%-${results_indent}s" "SORTED BY AVG." +echo "$results_header" +sort -g -k$((2*$NUM_DOMAINS2TEST + 2)) $results_tempfile +rm $results_tempfile + exit 0;