Tuesday, December 8, 2009

Poll .co.za domains for renewals.

Code is a bit clunky and can be improved upon , but I had to get this up and running after one of our providers forgot again to renew one of our domains.Put in a crontab to query nightly/weekly/monthtly.


#!/bin/bash

#########################################################################
# #
# Variables - Changeable #
# #
# #
#########################################################################


#Max amount of days before warning about lapse
warndays=30

#Domains to Query
domains="domain1.co.za domain2.co.za domain3.co.za"

#Monitor Email
emon="dnsmon@yourcompany.com"


########################################################################
# #
# #
# .CO.ZA Status Messages #
# #
# #
########################################################################
notpaid="NOTPAID"


#########################################################################
# #
# Variables - Non Changeable #
# #
# #
#########################################################################


#Variables
#Gets Todays Date
today=`date -d today +%s`


#########################################################################
# #
# #
# Run through the available domains and check if they're OK #
# #
# #
#########################################################################
clear


for d1 in $domains
do
echo "Now Processing $d1"
echo "Adding a pause on here so not to let .co.za think I am a robot"
let R=$RANDOM%60+180;echo $R
echo "Sleeping for $R"
sleep $R
whois $d1 > /tmp/$d1

#First check if account is not in arrears !
domainstatus=`egrep -i "\|" /tmp/"$d1" | tail -n 1 | cut -f 5 -d "|"| sed 's/[ \s]*//g'`


#Let's check if this domain has any unpaid fees
if [ "$domainstatus" = "$notpaid" ]
then
echo -e "\E[47;35m Domain $d1 has outstanding payments \033[0m"
echo -e `egrep -i "\|" /tmp/"$d1" | tail -n 1`
cat /tmp/"$d1" | mail "$emon" -s "Domain Outstanding Payments $d1"
echo
continue
fi

#Look at whether the PAID DATE field has a date in there
result=`egrep -i "\|" /tmp/$d1 | tail -n 1 | cut -f 5 -d "|"| sed 's/[ \s]*//g' | egrep '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'`
retval=$?

if [ "$retval" -lt 1 ]
then

#If domain seems to be paid let's continue and figure out when it will expire next year
regrenew=`egrep -i "\|" /tmp/"$d1" | tail -n 1 | cut -f 1 -d "|"| sed 's/[ \s]*//g'`
incyear=`echo $regrenew | cut -f 1 -d "-"`
incyear=`expr "$incyear" + 1`
suffixm=`echo $regrenew | cut -f 2 -d "-"`
suffixd=`echo $regrenew | cut -f 3 -d "-"`
regrenew="$incyear"-"$suffixm"-"$suffixd"
echo -e "New Domain date is \033[4m$regrenew \033[0m"

#Ok , now lets figure out how many days until we have until we have to re-register
registered=`date -d $regrenew +%s`
remaining=`expr "$registered" - "$today"`
remaining=`expr "$remaining" / 86400`
echo -e "\033[1mThere are $remaining days remaining before renewal \033[0m"
status=`egrep -i "\|" /tmp/"$d1" | tail -n 1`
echo $status


#Check if we have hit the warndays trigger
if [ $remaining -le $warndays ]
then
echo -e "Domain $d1 will expire in $remaining days ! ! ! \nPlease attend to this"| mail "$emon" -s "Domain Expiry $d1"

fi

fi

if [ "$retval" -gt 0 ]
then
echo -e "\E[47;35mThere is something wrong with your domain please inspect the following for any errors \033[0m"
echo -e `egrep -i "\|" /tmp/"$d1" | tail -n 1`
cat /tmp/"$d1" | mail "$emon" -s "Serious Domain Problem $d1"
fi


echo
echo
done

Followers