Skip to content

Instantly share code, notes, and snippets.

@iaean
Last active May 30, 2017 09:14
Show Gist options
  • Save iaean/f7f1d5d6cd8190db618e1a3d49a1e2f3 to your computer and use it in GitHub Desktop.
Save iaean/f7f1d5d6cd8190db618e1a3d49a1e2f3 to your computer and use it in GitHub Desktop.
Check DNS resolver reachability for all local IP addresses

A shell stub to check if all configured DNS resolver are reachable from all local IP addresses. It tries to search CNAME or A for an example query by binding the query to all local IPs. Remember: NXDOMAIN is a valid result. It's like a DNS ping...

cknsc.sh [www.example.com]

Config

Set CACHE to the IPs you like to test.

Some TODOs

  • better error checking
  • more things configurable
  • reliable output
  • IPv6
  • reverse lookup??
  • ...
#!/bin/bash
which uname ip awk dig tr >/dev/null 2>&1
if [ $? -gt 0 ]; then
exit -1
fi
CACHE="8.8.4.4 8.8.8.8"
QUERY=${1:-www.google.com}
host=`uname -n`
ipv4=`ip -4 -o a | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub(/\/.*/, " ",$4); print $2" "$4}'`
# IPv6 not implemented yet
# ipv6=`ip -6 -o a | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub(/\/.*/, " ",$4); print $2" "$4}'`
# your resolver from resolv.conf
for nsc in $CACHE
do
echo "$ipv4" | while read dev ip
do
# man dig(1)
result=`dig -4 @$nsc -b $ip +short $QUERY`
if [ $? != 9 ]; then
echo -n "OK [$nsc]: $ip@$dev@$host"
if [ -n "$result" ]; then
echo -n " | $QUERY -> $result" | tr '\n' ' '
fi
echo
else
# no response from cache
echo "FAILED [$nsc]: $ip@$dev@$host"
fi
done
done
domain example.com
search foo.example.com bar.example.com
options rotate timeout:2 attempts:2 single-request
nameserver 8.8.4.4
nameserver 8.8.8.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment