DNS check forward and reverse lookups

$ cat forward-reverse-dns-check.sh 
 
 
#!/bin/bash
#
# test DNS forward- and reverse-mapping
# Written Andrew Stringer July 2010 onwards... may even work now.
 
# edit this variable to reflect local class C subnet(s)
NETS="10.100.220"
 
# Test address to name to address validity
echo "Test ipaddress to host name lookup and then check hostname lookup to ipaddress."
echo
 
echo -e "\tIP address (PTR) -> hostname (A) -> IP address"
echo '-------------------------------------'
for NET in $NETS; do
  for n in $(seq 1 254); do
    ADDRESS=${NET}.${n}
    HOST=$(dig -x $ADDRESS +short)
    if test -n "$HOST"; then
      echo ${HOST} >> named-hosts
 
      ADDR=$(dig $HOST +short)
      if test "$ADDRESS" = "$ADDR"; then
        echo -e "ok\t$ADDRESS -> $HOST -> $ADDR"
      elif test -n "$ADDR"; then
        echo -e "fail\t$ADDRESS -> $HOST -> $ADDR"
      else
        echo -e "fail\t$ADDRESS -> $HOST -> [unassigned]"
      fi
    fi
  done
done
 
#get rid of blank lines with strings
 
strings named-hosts > named-hosts1
 
# Test name to address to name validity
echo "Test hostname to ipaddress lookup and then lookup up name from ipaddress."
echo
echo -e "\thostname (A) -> ip address (PTR) -> hostname"
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 "ok\t$H -> $ADDR -> $HOST"
    elif test -n "$HOST"; then
      echo -e "fail\t$H -> $ADDR -> $HOST"
    else
      echo -e "fail\t$H -> $ADDR -> [unassigned]"
    fi
  else
    echo -e "fail\t$H -> [unassigned]"
  fi
done < named-hosts1
more named-hosts
banana.rainsbrook.pri.
pear.rainsbrook.co.uk.
spruce.rainsbrook.co.uk.
 
linux/dns-fowardrev.txt · Last modified: 05/12/2022 14:53 by andrew