Skip to content

Instantly share code, notes, and snippets.

@suresh-sendinblue
Last active November 23, 2023 09:14
Show Gist options
  • Save suresh-sendinblue/512f0b917da1aae202f2fcf11f1300fb to your computer and use it in GitHub Desktop.
Save suresh-sendinblue/512f0b917da1aae202f2fcf11f1300fb to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
NS=$1
QUERIES=$2
CMD=$3
command -v bc > /dev/null || { echo "error: bc was not found. Please install bc."; exit 1; }
{ command -v drill > /dev/null && dig=drill; } || { command -v dig > /dev/null && dig=dig; } || { echo "error: dig was not found. Please install dnsutils."; exit 1; }
NAMESERVERS=`cat /etc/resolv.conf | grep ^nameserver | cut -d " " -f 2 | sed 's/\(.*\)/&#&/'`
PROVIDERSV4="
$NS
"
PROVIDERSV6="
2606:4700:4700::1111#cloudflare-v6
2001:4860:4860::8888#google-v6
2620:fe::fe#quad9-v6
2620:119:35::35#opendns-v6
2a0d:2a00:1::1#cleanbrowsing-v6
2a02:6b8::feed:0ff#yandex-v6
2a00:5a60::ad1:0ff#adguard-v6
2610:a1:1018::3#neustar-v6
"
# Testing for IPv6
$dig +short +tries=1 +time=2 +stats @2a0d:2a00:1::1 www.google.com |grep 216.239.38.120 >/dev/null 2>&1
if [ $? = 0 ]; then
hasipv6="true"
fi
providerstotest=$PROVIDERSV4
if [ "x$1" = "xipv6" ]; then
if [ "x$hasipv6" = "x" ]; then
echo "error: IPv6 support not found. Unable to do the ipv6 test."; exit 1;
fi
providerstotest=$PROVIDERSV6
elif [ "x$1" = "xipv4" ]; then
providerstotest=$PROVIDERSV4
elif [ "x$1" = "xall" ]; then
if [ "x$hasipv6" = "x" ]; then
providerstotest=$PROVIDERSV4
else
providerstotest="$PROVIDERSV4 $PROVIDERSV6"
fi
else
providerstotest=$PROVIDERSV4
fi
# 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"
DOMAINS2TEST="$QUERIES"
totaldomains=0
printf "%-21s" ""
for d in $DOMAINS2TEST; do
totaldomains=$((totaldomains + 1))
printf "%-8s" "test$totaldomains"
done
printf "%-8s" "Average"
echo ""
for p in $providerstotest; do
pip=${p%%#*}
pname=${p##*#}
ftime=0
printf "%-21s" "$pname"
for d in $DOMAINS2TEST; do
ttime=`$CMD $dig +tries=1 +time=2 +stats @$pip $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
elif [ "x$ttime" = "x0" ]; then
ttime=1
fi
printf "%-8s" "$ttime ms"
ftime=$((ftime + ttime))
done
avg=`bc -l <<< "scale=2; $ftime/$totaldomains"`
echo " $avg"
done
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment