Test your DNS with DIG

If you have ever maintained your own DNS, you’ll know it can be a bit of a ‘bind’. A typographic error can have catastrophic repercussions. I stumbled across a sweet shell script a few moths ago which I use to test the configuration of my DNS server.

The script utilises the Linux ‘dig’ command. The dig command is quite nicely described in its man page:

dig (domain information groper) is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.

In the context of the post, I use dig to perform a forward lookup e.g. host-name->IP address.  This achieved by executing the following command:

dig http://www.google.com

This returns google’s IP –  Dig also permits you to preform a reverse lookup e.g. IP->host-name

dig -x

This returns the host-name of the IP in question – google.com.  Thus, you can perform a circular test of your domains by combining both commands. Hopefully, you can see were I’m going now!  The shell script I stumbled up-on simply iterates around the 253 host of a class C network, printing the state of the forward and reserve lookup of each host in a tabular output.

Here is the script it all its glory:

# test DNS forward- and reverse-mapping#
# edit this variable to reflect local class C subnet(s)
NETS="192.168.1 192.168.2 192.168.3"
# Test address to name to address validity
echo -e "\taddress -> name -> address"
echo '-------------------------------------'
for NET in $NETS; do
for n in $(seq 1 254); do
HOST=$(dig -x $A +short)
if test -n "$HOST"; then
ADDR=$(dig $HOST +short)
if test "$A" = "$ADDR"; then
echo -e "ok\t$A -> $HOST -> $ADDR"
elif test -n "$ADDR"; then
echo -e "fail\t$A -> $HOST -> $ADDR"
echo -e "fail\t$A -> $HOST -> [unassigned]"

Sample output:

address -> name -> address


ok -> pluto.planets.local. ->

fail -> mars.planets.local. -> [unassigned]

ok -> venus.planets.local. ->

ok -> uranus.planets.local. ->

fail -> earth.planets.local. ->

ok -> jupitor.planets.local. ->

As you can see the script identifies two errors in the output above.  The first failure indicates the host ‘mars.planets.local’ has no reverse lookup.  The second shows a mismatch between the forward and reverse entries for the host ‘earth.planets.local’ .


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s