Wednesday, November 19, 2014

Shell Script To Check A Record For Given Domain or Hostname On All Nameservers

  1. #!/bin/bash
  2. # Shell script to check A record for given domain or hostname on all Nameservers
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2002 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. # How do I use this script?
  11. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. # ./script nixcraft.net 3 cyberciti.biz ns
  13. # where,
  14. # * nixcraft.net - nameserver domain name
  15. # * 3 - total number of nameserver
  16. # * cyberciti.biz - Domain name to check for A record
  17. # * ns - nameserver domain prefix, so ns becomes ns1.nixcraft.net,ns2.nixcraft.net,ns3.nixcraft.net
  18. # It will check cyberciti.biz domain on all 3 nameservers i.e. ns{1,2,3}.nixcraft.net
  19. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. # Note: Only tested on GNU/Linux but should work under BSD / UNIX like oses.
  21. # Last updated on: Jul-30-2008: Added failsafe and nameserver test.
  22. #-----------------------------------------------------------------------
  23. DOMAIN="$1" # nameserver domain
  24. MAX="$2" # query ns
  25. AREC="$3" # domain or hostname to check against given nameserver
  26. NS="$4"
  27.  
  28. # make sure we get args
  29. if [ ! $# -ge 3 ]
  30. then
  31. echo "$(basename $0) nameserver-domain number-of-nameservers domain-name-to-check nameserver-domain-prefix"
  32. echo "Example: Check domain viveks.org on all 3 nameservers ns1.nixcraft.net, ns2.nixcraft.net, ns2.nixcraft.net, enter:"
  33. echo "$(basename $0) nixcraft.net 3 viveks.org"
  34. exit 1
  35. fi
  36.  
  37. # shell array to hold all A records
  38. IPArray[0]=0
  39.  
  40. # failsafe
  41. [ "$4" == "" ] && NS="ns"
  42.  
  43. # note seq may not be available on other UNIX like oses
  44. for n in $(seq 1 ${MAX})
  45. do
  46. # build nameserver name
  47. thisNs="${NS}${n}.${DOMAIN}"
  48. # make sure nameserver exits
  49. host $thisNs | grep "NXDOMAIN" >/dev/null
  50. if [ $? -eq 0 ]
  51. then
  52. echo "Nameserver $thisNs does not exits..."
  53. exit 2
  54. fi
  55. # get output
  56. out=$(host $AREC $thisNs | grep address)
  57. # get A record
  58. IPArray[$n]=$(echo $out | awk '{print $4}')
  59. [ "$out" != "" ] && echo "${out} [$thisNs]"
  60. done
  61.  
  62. firstIp="$(echo ${IPArray[1]})"
  63. isARecSame=0
  64.  
  65. # use for loop to see all A records
  66. for (( i=1; i<=$MAX; i++ ));
  67. do
  68. [ "$firstIp" != "${IPArray[$i]}" ] && isARecSame=1 || :
  69. done
  70.  
  71. [ $isARecSame -eq 1 ] && echo "Warning: A record is not matching on all nameserver(s)" || :
  72.  

0 comments: