#!/bin/bash
# Author: Luca Zorzi <luca A_T tuttoeniente D_O_T net>
# License: CC Attribution (http://creativecommons.org/licenses/by/3.0/)
# A little script to compare 2 DNS servers

if [[ "$#" != "2" ]]
then
 echo "USAGE: $0 dns-server-1 dns-server-2"
 exit 1
fi

start=`date +%s`

sites[1]="google.com"
sites[2]="ubuntu.com"
sites[3]="kernel.org"

dns_index=0;
for DNS in $1 $2
do
 dns_index=`echo "$dns_index + 1" | bc`
 dns_average[$dns_index]=0;
 site_index=0;
 for site in "${sites[@]}";
 do
  site_index=`echo "$site_index + 1" | bc`
  total=0
  for n in 1 2 3 4 5
  do
   result=`dig @$DNS $site| grep "Query time" | awk '{print $4}'`
   total=`echo "$total + $result" | bc`
  done
  old_avg=${dns_average[$dns_index]}
  dns_average[$dns_index]=`echo "$old_avg + $total" | bc`
 done
  total=${dns_average[$dns_index]}
  average[$dns_index]=`echo "$total / ($site_index * 5)" | bc`
done

end=`date +%s`
runtime=`echo "$end - $start" | bc`

echo "TEST FINISHED"
echo "Run time: $runtime seconds"
echo "Testing $1 vs. $2"
echo "----------------------------------"
echo "$1 average: ${average[1]} ms"
echo "$2 average: ${average[2]} ms"
