PDA

View Full Version : Linux bash script to check list of domains and email alert once domain is available



Fli
11-30-2014, 05:19 PM
This script (domexpcheck.sh) is doing check acros file named domlistexp. This file contains domains that are in Expired state (meaning they are not yet available for registration)

- if any of the domains become taken/renewed, its added to the domlist
- if any domain becomes available for registration, its added to the domlistavail and email is sent to you notifying some domain is available

The script:

#!/bin/bash
#export PATH=/usr/bin:/bin
#set -ex
# domain availability checker
#
# HOW TO USE:
# 1) name this script is "domexpcheck.sh" and add it into /root/domchck (path mentioned on the following line)
# 2) into /root/domchck/domlistexp add expired domain names
# 3) edit "maill" variable below to setup your mail address
# 4) setup like every 3 minutes cronjob: */3 * * * * /bin/sh /root/domchck/domexpcheck.sh > /dev/null
#
cd /root/domchck
[email protected]

cantfindns="server can"
canfindns="Authoritative answers can be found from"
nslookupnsfailureinzone="Got SERVFAIL reply from"
cznicnoentries="No entries found."
sknicnotfound="Not found."
verisign="No match for domain"
verisignalternative="No match $domain"
eurid="Status: AVAILABLE"
afiliasinfo="NOT FOUND"
nominetuk="This domain name has not been registered."
isavail="0"
isexp="0"
domlist=domlist
domlistexp=domlistexp
domlistavail=domlistavail
domlistavail_bak=domlistavail_bak
domlist_bak=domlist_bak
domlistexp_bak=domlistexp_bak
touch $domlist $domlistexp $domlistavail
rm -rf $domlist_bak $domlistexp_bak $domlistavail_bak
if [ "$(ps aux | grep domexpcheck | grep -v grep | wc -l)" -gt "2" ];then
echo "It appears there is more than one domexpcheck script running so exitting this script now. yes, 2 is not a mistake"
exit
fi

for domain in $(cat $domlistexp);do
nslookupout=$(nslookup -type=ns $domain)

# --------- IS EXPIRED/AVAILABLE OR TAKEN? --------

if [[ "$nslookupout" == *"$cantfindns"* ]];then
isexp="1"
echo "NSs for domain $domain cant be found. So its expired, available or NSs wrongly configured (= TAKEN in zone)"
#.....Ok We check if -Got SERVFAIL reply from- is in nslookup, so it can mean NS wrongly configured, domain in zone
fi
if [[ "$nslookupout" == *"$nslookupnsfailureinzone"* ]];then
isexp="0"
fi

echo "$domain has isexp: $isexp"

if [ "$isexp" != "111" ];then
# above rule was ==1

echo "Ok We will do whois lookup to veriffy if its expired or available"

whoisout=$(whois $domain -f -n)

if [[ "$whoisout" == *"$cznicnoentries"* ]];then
isavail="1"
echo "Domain $domain is available at cznic"
elif [[ "$whoisout" == *"$sknicnotfound"* ]];then
isavail="1"
echo "Domain $domain is available at sknic"
elif [[ "$whoisout" == *"$verisign"* ]];then
isavail="1"
echo "Domain $domain is available at verisign"
elif [[ "$whoisout" == *"$verisignalternative"* ]];then
isavail="1"
echo "Domain $domain is available at verisignalternative"
elif [[ "$whoisout" == *"$eurid"* ]];then
isavail="1"
echo "Domain $domain is available at eurid"
elif [[ "$whoisout" == *"$afiliasinfo"* ]];then
isavail="1"
echo "Domain $domain is available at inforegistar afilias"
elif [[ "$whoisout" == *"$nominetuk"* ]];then
isavail="1"
echo "Domain $domain is available at nominet UK"
else
isavail="0"
fi

if [[ "$nslookupout" == *"$canfindns"* ]];then
isexp="0"
nsfoundd="It appears domain has nameservers. There was no NSs fail, misconfiguration. Does it mean domain is taken? was this domain removed from domexplist and moved to domlist?"
echo "$nsfoundd" | mail -s "NSs found for $domain" $maill
fi

# now we know if its avaialble or taken(isavail=0)

# ----- DOMAIN IS AVAILABLE + EXPIRED ACTION -----

if [ "$isavail" == "1" ];then

echo "Domain $domain is available now. Mail sent from hqscenes VPS, bash script.

Registrace .cz: https://www.cesky-hosting.cz/objednavka/
Registrace .com: http://godaddy.com
Coupons Godaddy: http://www.retailmenot.com/s/godaddy+domain" | mail -s "Domain $domain available!" [email protected]

# move domain to avail list and delete it from other lists
touch $domlistavail
echo "$domain" >> $domlistavail
# remove from other than avail list
#cp $domlist $domlist_bak;sed /^$domain$/d $domlist_bak > $domlist;rm -rf $domlist_bak
cp $domlistexp $domlistexp_bak;awk '!x[$0]++' $domlistexp_bak | sed /^$domain$/d > $domlistexp;rm -rf $domlistexp_bak

# ----- DOMAIN IS UNAVAILABLE + EXPIRED ACTION -----

fi

if [[ "$isavail" == "0" && "$isexp" == "0" ]];then

echo "Appears to be not expired and not available so its taken, lets move it to domlist and delete from domlistexp."

istakenresult="$domain appears to be taken (it probably contains nameservers (isexp=0)), domexpcheck failed to catch ontime? Veriffy please manually here if doman have NS,was renewed?: http://who.is/whois/$domain . If domain is still expired, it must be some error in my domexpcheck script. This domain was already moved back to unavailable list (domlist)

NSLOOKUP OUT:
$nslookupout

WHOIS OUT (if was needed):
$whoisout"
echo "$istakenresult" | mail -s "domexpcheck failed to catch $domain ?" $maill

#remove from exp list and add to domlist
touch $domlistexp
cp $domlistexp $domlistexp_bak;awk '!x[$0]++' $domlistexp_bak | sed /^$domain$/d > $domlistexp;rm -rf $domlistexp_bak
# add it to domlist if it dont exist in it already
if [ "$(cat $domlist | grep $domain)" == "0" ];then
echo "$domain" >> $domlist
fi

if [[ "$isavail" == "0" && "$isexp" == "1" ]];then

echo "Isavail=0 a isexp=1 it means domain is expired and not available, so we do nothing and continue checks next time"

fi

# ----- DOMAIN is not isexp=1 - has nameservers - IS TAKEN ACTION -----

#else
#echo "something else"
#fi

fi
fi
done
# cp medvidek medvidek2;rm -rf medvidek;cp $domlist medvidek;awk '!x[$0]++' medvidek; cat medvidek > $domlist


# COMMENTED OUT


# -------- IF AVAILABLE -------
#if [ "$isavail" == "1" ];then
#echo "Domain $domain is available now. Mail sent from hqscenes VPS, bash script.
#
#Registrace .cz: https://www.cesky-hosting.cz/objednavka/
#Registrace .com: http://godaddy.com" | mail -s "Domain $domain available!" [email protected]
#
# move domain to avail list and delete it from other lists
#touch $domlistavail
#echo "$domain" >> $domlistavail
# remove from other than avail list
#cp $domlist $domlist_bak;sed /^\$domain$/d $domlist_bak > $domlist;rm -rf $domlist_bak
#cp $domlistexp $domlistexp_bak;awk '!x[$0]++' $domlistexp_bak | sed /^\$domain$/d > $domlistexp;rm -rf $domlistexp_bak
#cp $domlistavail $domlistavail_bak;awk '!x[$0]++' $domlistavail_bak | sed /^\$domain$/d > $domlistavail;rm -rf $domlistavail_bak
#
# --------- IF EXPIRED --------
#elif [ "$isexp" == "1" ];then
#.....add domain to the file with expired, non available, domains which is checked frequently
#touch $domlistexp
# add it to domlistexp and deduplicate the file
#echo "$domain" >> $domlistexp
#cp $domlistexp $domlistexp_bak;awk '!x[$0]++' $domlistexp_bak > $domlistexp;rm -rf $domlistexp_bak
# remove from domlist
#cp $domlist $domlist_bak;sed /^\$domain$/d $domlist_bak > $domlist;rm -rf $domlist_bak
#fi
#
#sleep 4
#done < $domlist