Script to monitor DNS entries



Here's a script we're using to monitor the DNS entries for all our domains. This script is called once a day with a cron job.


The script reads a file containing a list of domains to monitor (one domain name per line). The path to this file can be configure with the variable DOMAINLIST.


Every time the script is run, it copies the results of the last run to OLDLOG (can be configured in the script) and writes the new results in NEWLOG (also configurable).


Then it performs a diff. If no changes occurred between the two runs, nothing happens. If something changed, an email is sent to the configured email addresses (variable EMAILS) containing the diff results.




#!/bin/bash


DOMAINLIST="/home/scripts/domains.list”


MAIL="/usr/bin/mail"
EMAILS="henri.benoit@gmail.com xxx.xxx@xxx.com"


OLDLOG="/root/monitordns.OLD"
NEWLOG="/root/monitordns.CURRENT"
TEMPLOG="/root/monitordns.$$"


echo> $OLDLOG
mv $NEWLOG $OLDLOG


while read line
do


echo "Checking $line:" | tee -a $NEWLOG
dig +nocomments $line ANY | grep -v ";" | grep -v "^$" | awk '{ $2=""; print "'$line': "$0; }' | sort | tee -a $NEWLOG
echo "" | tee -a $NEWLOG


done < $DOMAINLIST


echo "-------------------------------------------------------------"


diff -y --suppress-common-lines $OLDLOG $NEWLOG > $TEMPLOG


if [ -s $TEMPLOG ] ; then
  for EMAIL in $EMAILS
  do
    $MAIL -s "DNS status update" $EMAIL < $TEMPLOG
  done
fi
rm -f $TEMPLOG


Leave a Reply

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