Verifying DNS mappings

An improperly configured DNS setup can be really annoying. You want to make sure that your mappings work both ways:

  1. Each hostname should resolve to an address, and that address ought to resolve back to the proper hostname.
  2. If an address on your subnet(s) has been assigned a reverse pointer to a hostname, that hostname ought to point back to the original address.

There are exceptions to those two rules, of course. A CNAME will resolve to another hostname first, and only then to an address. Sometimes multiple hostnames will point to the same address, but that address will have only one reverse pointer.

Still, it’s good to know that your basic mappings work as expected.

You can script such a test if you build a file containing your known hostnames. The example script below is pretty simple; it will break if fed a CNAME, and it’ll report a failure somewhere if multiple hostnames point to the same address. Let’s assume the file containing your hostnames is named named-hosts.

#!/bin/bash
#
# test DNS forward- and reverse-mapping
#

# edit this variable to reflect local class C subnet(s)
NETS="192.168.1 192.168.2"

# Test name to address to name validity
echo
echo -e "tname -> address -> name"
echo '----------------------------------'
while read H; do
  ADDR=$(dig $H +short)
  if test -n "$ADDR"; then
    HOST=$(dig -x $ADDR +short)
    if test "$H" = "$HOST"; then
      echo -e "okt$H -> $ADDR -> $HOST"
    elif test -n "$HOST"; then
      echo -e "failt$H -> $ADDR -> $HOST"
    else
      echo -e "failt$H -> $ADDR -> [unassigned]"
    fi
  else
    echo -e "failt$H -> [unassigned]"
  fi
done < named-hosts

# Test address to name to address validity
echo
echo -e "taddress -> name -> address"
echo '-------------------------------------'
for NET in $NETS; do
  for n in $(seq 1 254); do
    A=${NET}.${n}
    HOST=$(dig -x $A +short)
    if test -n "$HOST"; then
      ADDR=$(dig $HOST +short)
      if test "$A" = "$ADDR"; then
        echo -e "okt$A -> $HOST -> $ADDR"
      elif test -n "$ADDR"; then
        echo -e "failt$A -> $HOST -> $ADDR"
      else
        echo -e "failt$A -> $HOST -> [unassigned]"
      fi
    fi
  done
done

Source: http://www.madboa.com