wei
6/29/2016 - 9:35 PM

Monitor whois changes

Monitor whois changes

#!/bin/bash

#
# Run this script once per minute
#
# * * * * * /path/to/whois-watch.sh google.com apple.com >> /var/log/whois-watch.log 2>&1
# update username in mail command
# update sed command depending on server return text
#

TMP_DIR="/tmp"

function TIMESTAMP() {
    echo $(date -u +"%Y-%m-%d-%H:%M:%S")
}
NEWLINE=$'\n'

for DOMAIN in "$@"
do
    echo "[whois-watch.sh][$(TIMESTAMP)] Checking whois for $DOMAIN"
    OLD="$TMP_DIR/$DOMAIN.old.whois.txt"
    NEW="$TMP_DIR/$DOMAIN.new.whois.txt"
    DIFF="$TMP_DIR/$DOMAIN.whois.diff.txt"
    
    if [ ! -f $OLD ]; then
        echo "not available" > $OLD
    fi
    whois $DOMAIN -H | sed -e 's/^.*last update.*$//gI' > $NEW
    OLDHASH=$(sha1sum $OLD | awk '{print $1}')
    NEWHASH=$(sha1sum $NEW | awk '{print $1}')
    
    if [ "$OLDHASH" != "$NEWHASH" ]; then
        echo "[whois-watch.sh][$(TIMESTAMP)] $DOMAIN WHOIS Updated"
        diff $NEW $OLD > $DIFF
        mail -s "Whois information of $DOMAIN changed at $TIMESTAMP UTC" root < $DIFF
        rm $DIFF
    else
        echo "[whois-watch.sh][$(TIMESTAMP)] $DOMAIN WHOIS Hasn't Changed"
    fi
    mv $NEW $OLD
done