A shell script to check domain serial numbers across all name servers

#!/bin/bash
# Linux shell script to check BIND named domain serial numbers across all name servers
# Tested on RHEL, Fedora, Centos and Debian Linux
# Requires named-checkzone, host utilities, and BIND server.
# -------------------------------------------------------------------------
# Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Last updated on : March-2009.
# -------------------------------------------------------------------------
### Set me ###
CZBASE=/etc/bind/zones
NAMED_CHKZON=/usr/sbin/named-checkzone
NS1=ns1.nixcraft.net
NS2=ns2.nixcraft.net
NS3=ns3.nixcraft.net
ZPREF=master
if [ $# -eq 0 ]
then
	echo "$0 domain-name"
	exit 1
fi
d=$1
ZONEFILE=${CZBASE}/${ZPREF}.${d}
if [  -f $ZONEFILE ] 
then
		S1=$(host -t soa $d $NS2 | grep "^$d" | awk '{ print $7 }')
		S2=$(host -t soa $d $NS3 | grep "^$d" | awk '{ print $7 }')
		M=$($NAMED_CHKZON -t $CZBASE $d ${ZPREF}.${d}| grep "$d" | awk '{ print $5 }')
		echo -e "$NS1 # $S1\n$NS2 # $S2\n$NS3 # $M"
		$NAMED_CHKZON -q -t $CZBASE $d ${ZPREF}.${d}
		[ $? -eq 0 ] && echo "$d : OK"
		[ $S1 -eq $S2 -a $S1 -eq $M -a $S2 -eq $S1 -a $S2 -eq $M -a $M -eq $S1 -a $M -eq $S2 ] \
			&& echo "$d : Serial numbers same!" || echo "$d : Serial number different, reload named!"
else
		echo "Error - $d domain or $ZONEFILE zone file does not exits!"
fi

Sample output

$ ./zonev cyberciti.biz
Output:

ns1.nixcraft.net # 2008072318
ns2.nixcraft.net # 2008072318
ns3.nixcraft.net # 2008072318
cyberciti.biz : OK
cyberciti.biz : Serial numbers same!
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • anu Aug 30, 2010 @ 10:44

    wat does dis line of code exactly does
    function logecho
    {
    echo “$*” | sed “s/^/[$(date ‘+%Y.%m.%d %H:%M:%S’)] /”

    return $?
    }

  • angath Aug 21, 2009 @ 7:57

    Could any one explain what the following line does in the code?

    echo -e “$NS1 # $S1\n$NS2 # $S2\n$NS3 # $M”
    $NAMED_CHKZON -q -t $CZBASE $d ${ZPREF}.${d}
    [ $? -eq 0 ] && echo “$d : OK”
    [ $S1 -eq $S2 -a $S1 -eq $M -a $S2 -eq $S1 -a $S2 -eq $M -a $M -eq $S1 -a $M -eq $S2 ] \

    • 🐧 Vivek Gite Aug 21, 2009 @ 12:32

      Print all 3 name servers:

      echo -e “$NS1 # $S1\n$NS2 # $S2\n$NS3 # $M”

      Check zone file

      $NAMED_CHKZON -q -t $CZBASE $d ${ZPREF}.${d}

      Report it back:

      [ $? -eq 0 ] && echo “$d : OK”

      If all serial numbers are same on all name servers give message, else ask to reload master named.

      [ $S1 -eq $S2 -a $S1 -eq $M -a $S2 -eq $S1 -a $S2 -eq $M -a $M -eq $S1 -a $M -eq $S2 ] \

      HTH

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.