diff -r 14a903cf9505 -r 4bccc75a8a1c scripts/domain-check --- a/scripts/domain-check Mon Jan 14 17:18:11 2013 -0500 +++ b/scripts/domain-check Fri Jun 28 13:02:43 2013 -0400 @@ -8,6 +8,11 @@ # Current Version: 1.95 # # Revision History: +# Version 1.96 +# Rename variables so system variables aren't potentially changed. -- Eric Renfro +# Code optimization and per-whois delay capability is possible. -- Eric Renfro +# Output table is now fully string size padded for results. -- Eric Renfro + # Version 1.95 # Added support for .ie and .us domain names # Cleaned up some areas where awk and cut were being called @@ -16,7 +21,7 @@ # Bug fix and enhancement for .uk and .co.uk -- Vivek Gite # # Version 1.8 -# Bug fix added $MAIL -- Vivek Gite +# Bug fix added $mailContact -- Vivek Gite # # Version 1.7 # Added support for .jp domain names -- Vivek Gite @@ -110,31 +115,41 @@ PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/ssl/bin:/usr/sfw/bin ; export PATH # Who to page when an expired domain is detected (cmdline: -e) -ADMIN="sysadmin@mydomain.com" +admin="sysadmin@mydomain.com" + +# Hostname to report to Zabbix Servers (cmdline: -i) +hostname="-" # Number of days in the warning threshhold (cmdline: -x) -WARNDAYS=30 +warnDays=30 -# If QUIET is set to TRUE, don't print anything on the console (cmdline: -q) -QUIET="FALSE" +# If quiet is set to TRUE, don't print anything on the console (cmdline: -q) +quiet=false + +# Human readable output default to off for Zabbix server processing (cmdline: -r) +human=false # Don't send emails by default (cmdline: -a) -ALARM="FALSE" +alarm=false + +# JSON Discovery mode off by default (cmdline: -j) +json=false # Whois server to use (cmdline: -s) -WHOIS_SERVER="whois.internic.org" +whoisServer="whois.internic.org" # Location of system binaries -AWK="/usr/bin/awk" -WHOIS="/usr/bin/whois" -DATE="/bin/date" -CUT="/usr/bin/cut" -MAIL="/bin/mail" -# Place to stash temporary files -WHOIS_TMP="/var/tmp/whois.$$" +awk="/usr/bin/awk" +whois="/usr/bin/whois" +date="/bin/date" +cut="/usr/bin/cut" +mailContact="/bin/mail" + +# Track tld types for query limiting: +declare -A domainPoll ############################################################################# -# Purpose: Convert a date from MONTH-DAY-YEAR to Julian format +# Purpose: Convert a date from month-DAY-YEAR to Julian format # Acknowledgements: Code was adapted from examples in the book # "Shell Scripting Recipes: A Problem-Solution Approach" # ( ISBN 1590594711 ) @@ -145,7 +160,7 @@ ############################################################################# date2julian() { - if [ "${1} != "" ] && [ "${2} != "" ] && [ "${3}" != "" ] + if [[ "${1} != "" && "${2} != "" && "${3}" != "" ]] then ## Since leap years add aday at the end of February, ## calculations are done from 1 March 0000 (a fictional year) @@ -170,9 +185,7 @@ ############################################################################# getmonth() { - LOWER=`tolower $1` - - case ${LOWER} in + case ${1,,} in jan) echo 1 ;; feb) echo 2 ;; mar) echo 3 ;; @@ -185,7 +198,7 @@ oct) echo 10 ;; nov) echo 11 ;; dec) echo 12 ;; - *) echo 0 ;; + *) echo 0 ;; esac } @@ -197,7 +210,7 @@ ############################################################################# date_diff() { - if [ "${1}" != "" ] && [ "${2}" != "" ] + if [[ "${1}" != "" && "${2}" != "" ]] then echo $(expr ${2} - ${1}) else @@ -206,214 +219,185 @@ } ################################################################## -# Purpose: Converts a string to lower case -# Arguments: -# $1 -> String to convert to lower case -################################################################## -tolower() -{ - LOWER=`echo ${1} | tr [A-Z] [a-z]` - echo $LOWER -} - -################################################################## # Purpose: Access whois data to grab the registrar and expiration date # Arguments: # $1 -> Domain to check ################################################################## check_domain_status() { - local REGISTRAR="" - # Avoid WHOIS LIMIT EXCEEDED - slowdown our whois client by adding 3 sec - sleep 3 + local registrar="" + # Avoid whois LIMIT EXCEEDED - slowdown our whois client by adding 3 sec + #sleep 3 # Save the domain since set will trip up the ordering - DOMAIN=${1} - TLDTYPE="`echo ${DOMAIN} | ${CUT} -d '.' -f3 | tr '[A-Z]' '[a-z]'`" - if [ "${TLDTYPE}" == "" ]; + domain=${1} + tldType="$(echo ${domain} | ${cut} -d '.' -f3 | tr '[A-Z]' '[a-z]')" + if [[ "${tldType}" = "" ]]; then - TLDTYPE="`echo ${DOMAIN} | ${CUT} -d '.' -f2 | tr '[A-Z]' '[a-z]'`" + tldType="$(echo ${domain} | ${cut} -d '.' -f2 | tr '[A-Z]' '[a-z]')" fi # Invoke whois to find the domain registrar and expiration date - #${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP} + #whoisVal=$(${whois} -h ${whoisServer} "=${1}") # Let whois select server - if [ "${TLDTYPE}" == "org" ]; - then - ${WHOIS} -h "whois.pir.org" "${1}" > ${WHOIS_TMP} - elif [ "${TLDTYPE}" == "in" ]; # India - then - ${WHOIS} -h "whois.registry.in" "${1}" > ${WHOIS_TMP} - elif [ "${TLDTYPE}" == "uk" ]; # United Kingdom - then - ${WHOIS} -h "whois.nic.uk" "${1}" > ${WHOIS_TMP} + + # The whois Expiration data should resemble the following: "Expiration Date: 09-may-2008" + case $tldType in + "org") [[ -n ${domainPoll[$tldType]} ]] && sleep 15 + whoisVal=$(${whois} -h "whois.pir.org" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration Date:/ { print $2 }' | ${cut} -d':' -f2) + ;; + "in") whoisVal=$(${whois} -h "whois.registry.in" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration Date:/ { print $2 }' | ${cut} -d':' -f2) + ;; + "uk") whoisVal=$(${whois} -h "whois.nic.uk" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Renewal date:/ || /Expiry date:/ { print $3 }') + ;; + "biz") whoisVal=$(${whois} -h "whois.neulevel.biz" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }') + ;; + "info") whoisVal=$(${whois} -h "whois.afilias.info" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration Date:/ { print $2 }' | ${cut} -d':' -f2) + ;; + "us") whoisVal=$(${whois} -h "whois.nic.us" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }') + ;; + "com" | "net" | "edu") + whoisVal=$(${whois} -h "whois.internic.org" "${1}") + registrar=$(echo "$whoisVal" | grep "Registrar:" | sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g' | cut -d':' -f2 | sed -e 's/^[ \t]*//g') + domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration/ { print $NF }') - elif [ "${TLDTYPE}" == "biz" ]; - then - ${WHOIS} -h "whois.neulevel.biz" "${1}" > ${WHOIS_TMP} - elif [ "${TLDTYPE}" == "ie" ]; # Ireland - then - ${WHOIS} -h "whois.domainregistry.ie" "${1}" > ${WHOIS_TMP} - elif [ "${TLDTYPE}" == "info" ]; - then - ${WHOIS} -h "whois.afilias.info" "${1}" > ${WHOIS_TMP} - elif [ "${TLDTYPE}" == "jp" ]; # Japan - then - ${WHOIS} -h "whois.jprs.jp" "${1}" > ${WHOIS_TMP} - elif [ "${TLDTYPE}" == "us" ]; - then - ${WHOIS} -h "whois.nic.us" "${1}" > ${WHOIS_TMP} + #echo "Registrar = $registrar" + #exit 0 + ;; + "ie") whoisVal=$(${whois} -h "whois.domainregistry.ie" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + tdomdate=$(echo "${whoisVal}" | ${awk} '/renewal/ { print $2 }') + tyear=$(echo ${tdomdate} | ${cut} -d'-' -f3) + tmon=$(echo ${tdomdate} | ${cut} -d'-' -f2) + case ${tmon} in + "January") tmonth=jan ;; + "February") tmonth=feb ;; + "March") tmonth=mar ;; + "April") tmonth=apr ;; + "May") tmonth=may ;; + "June") tmonth=jun ;; + "July") tmonth=jul ;; + "August") tmonth=aug ;; + "September") tmonth=sep ;; + "October") tmonth=oct ;; + "November") tmonth=nov ;; + "December") tmonth=dec ;; + *) tmonth=0 ;; + esac + tday=$(echo ${tdomdate} | ${cut} -d'-' -f1) + domainDate=$(echo $tday-$tmonth-$tyear) + ;; + "jp") whoisVal=$(${whois} -h "whois.jprs.jp" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + tdomdate=$(echo "${whoisVal}" | ${awk} '/Expires on/ { print $3 }') + tyear=$(echo ${tdomdate} | ${cut} -d'/' -f1) + tmon=$(echo ${tdomdate} | ${cut} -d'/' -f2) + case ${tmon} in + 1|01) tmonth=jan ;; + 2|02) tmonth=feb ;; + 3|03) tmonth=mar ;; + 4|04) tmonth=apr ;; + 5|05) tmonth=may ;; + 6|06) tmonth=jun ;; + 7|07) tmonth=jul ;; + 8|08) tmonth=aug ;; + 9|09) tmonth=sep ;; + 10) tmonth=oct ;; + 11) tmonth=nov ;; + 12) tmonth=dec ;; + *) tmonth=0 ;; + esac + tday=$(echo ${tdomdate} | ${cut} -d'/' -f3) + domainDate=$(echo $tday-$tmonth-$tyear) + ;; + "cz") whoisVal=$(${whois} -h "${whoisServer}" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + tdomdate=$(echo "${whoisVal}" | awk '/expire:/ { print $2 }') + tyear=$(echo ${tdomdate} | cut -d'.' -f3) + tmon=$(echo ${tdomdate} | cut -d'.' -f2) + tday=$(echo ${tdomdate} | cut -d'.' -f1) + case ${tmon} in + 1|01) tmonth=jan ;; + 2|02) tmonth=feb ;; + 3|03) tmonth=mar ;; + 4|04) tmonth=apr ;; + 5|05) tmonth=may ;; + 6|06) tmonth=jun ;; + 7|07) tmonth=jul ;; + 8|08) tmonth=aug ;; + 9|09) tmonth=sep ;; + 10) tmonth=oct ;; + 11) tmonth=nov ;; + 12) tmonth=dec ;; + *) tmonth=0 ;; + esac + domainDate=$(echo $tday-$tmonth-$tyear) + ;; + *) whoisVal=$(${whois} -h "${whoisServer}" "${1}") + registrar=$(echo "$whoisVal" | grep "Sponsoring Registrar:" | cut -d':' -f2) + domainDate=$(echo "${whoisVal}" | ${awk} '/Expiration/ { print $NF }') + ;; + esac - elif [ "${TLDTYPE}" == "com" -o "${TLDTYPE}" == "net" -o "${TLDTYPE}" == "edu" ]; - then - ${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP} + if [[ -z ${domainPoll[$tldType]} ]]; then + domainPoll[$tldType]=1 else - ${WHOIS} "${1}" > ${WHOIS_TMP} - fi - - # Parse out the expiration date and registrar -- uses the last registrar it finds - REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar/ && $2 != "" { REGISTRAR=substr($2,2,17) } END { print REGISTRAR }'` - - if [ "${TLDTYPE}" == "uk" ]; # for .uk domain - then - REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $0 != "" { getline; REGISTRAR=substr($0,2,17) } END { print REGISTRAR }'` - elif [ "${TLDTYPE}" == "jp" ]; - then - REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/Registrant/ && $2 != "" { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'` - elif [ "${TLDTYPE}" == "cz" ]; - then - REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/registrar:/ && $2 != "" { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'` - elif [ "${TLDTYPE}" == "ie" ]; # for .ie domain - then - REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/source/ && $2 != "" { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'` - elif [ "${TLDTYPE}" == "us" ]; - then - REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Last Updated by Registrar:/ && $2 != "" { REGISTRAR=substr($2,20,17) } END { print REGISTRAR }'` + let domainPoll[$tldType]++ fi # If the Registrar is NULL, then we didn't get any data - if [ "${REGISTRAR}" = "" ] - then - prints "$DOMAIN" "Unknown" "Unknown" "Unknown" "Unknown" - return - fi - - # The whois Expiration data should resemble the following: "Expiration Date: 09-may-2008" + #if [[ "${registrar}" = "" ]] + #then + # prints "$domain" "Unknown" "Unknown" "Unknown" "Unknown" + # return + #fi - # for .in, .info, .org domains - if [ "${TLDTYPE}" == "in" -o "${TLDTYPE}" == "info" -o "${TLDTYPE}" == "org" ]; - then - DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration Date:/ { print $2 }' | ${CUT} -d':' -f2` - elif [ "${TLDTYPE}" == "biz" ]; # for .biz domain - then - DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'` - elif [ "${TLDTYPE}" == "ie" ]; # for .ie domain - then - tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/renewal/ { print $2 }'` - tyear=`echo ${tdomdate} | ${CUT} -d'-' -f3` - tmon=`echo ${tdomdate} | ${CUT} -d'-' -f2` - case ${tmon} in - "January") tmonth=jan ;; - "February") tmonth=feb ;; - "March") tmonth=mar ;; - "April") tmonth=apr ;; - "May") tmonth=may ;; - "June") tmonth=jun ;; - "July") tmonth=jul ;; - "August") tmonth=aug ;; - "September") tmonth=sep ;; - "October")tmonth=oct ;; - "November") tmonth=nov ;; - "December") tmonth=dec ;; - *) tmonth=0 ;; - esac - tday=`echo ${tdomdate} | ${CUT} -d'-' -f1` - DOMAINDATE=`echo $tday-$tmonth-$tyear` - elif [ "${TLDTYPE}" == "uk" ]; # for .uk domain - then - DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Renewal date:/ || /Expiry date:/ { print $3 }'` - elif [ "${TLDTYPE}" == "us" ]; # for .us domain - then - DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'` - elif [ "${TLDTYPE}" == "jp" ]; # for .jp 2010/04/30 - then - tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expires on/ { print $3 }'` - tyear=`echo ${tdomdate} | ${CUT} -d'/' -f1` - tmon=`echo ${tdomdate} | ${CUT} -d'/' -f2` - case ${tmon} in - 1|01) tmonth=jan ;; - 2|02) tmonth=feb ;; - 3|03) tmonth=mar ;; - 4|04) tmonth=apr ;; - 5|05) tmonth=may ;; - 6|06) tmonth=jun ;; - 7|07) tmonth=jul ;; - 8|08) tmonth=aug ;; - 9|09) tmonth=sep ;; - 10)tmonth=oct ;; - 11) tmonth=nov ;; - 12) tmonth=dec ;; - *) tmonth=0 ;; - esac - tday=`echo ${tdomdate} | ${CUT} -d'/' -f3` - DOMAINDATE=`echo $tday-$tmonth-$tyear` - elif [ "${TLDTYPE}" == "cz" ]; # for .cz 08.02.2013 dd.mm.yyyy - then - tdomdate=`cat ${WHOIS_TMP} | awk '/expire:/ { print $2 }'` - tyear=`echo ${tdomdate} | cut -d'.' -f3` - tmon=`echo ${tdomdate} | cut -d'.' -f2` - tday=`echo ${tdomdate} | cut -d'.' -f1` - case ${tmon} in - 1|01) tmonth=jan ;; - 2|02) tmonth=feb ;; - 3|03) tmonth=mar ;; - 4|04) tmonth=apr ;; - 5|05) tmonth=may ;; - 6|06) tmonth=jun ;; - 7|07) tmonth=jul ;; - 8|08) tmonth=aug ;; - 9|09) tmonth=sep ;; - 10) tmonth=oct ;; - 11) tmonth=nov ;; - 12) tmonth=dec ;; - *) tmonth=0 ;; - esac - DOMAINDATE=`echo $tday-$tmonth-$tyear` - else # .com, .edu, .net and may work with others - DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration/ { print $NF }'` - fi - - #echo $DOMAINDATE # debug + #echo $domainDate # debug # Whois data should be in the following format: "13-feb-2006" IFS="-" - set -- ${DOMAINDATE} - MONTH=$(getmonth ${2}) + set -- ${domainDate} + month=$(getmonth ${2}) IFS="" # Convert the date to seconds, and get the diff between NOW and the expiration date - DOMAINJULIAN=$(date2julian ${MONTH} ${1#0} ${3}) - DOMAINDIFF=$(date_diff ${NOWJULIAN} ${DOMAINJULIAN}) + domainJulian=$(date2julian ${month} ${1#0} ${3}) + domainDiff=$(date_diff ${nowJulian} ${domainJulian}) - if [ ${DOMAINDIFF} -lt 0 ] + if ! $human then - if [ "${ALARM}" = "TRUE" ] - then - echo "The domain ${DOMAIN} has expired!" \ - | ${MAIL} -s "Domain ${DOMAIN} has expired!" ${ADMIN} - fi + echo "$hostname domain.daysleft.${domain} ${domainDiff}" + fi - prints ${DOMAIN} "Expired" "${DOMAINDATE}" "${DOMAINDIFF}" ${REGISTRAR} - - elif [ ${DOMAINDIFF} -lt ${WARNDAYS} ] + if [[ ${domainDiff} -lt 0 ]] + then + if $alarm + then + echo "The domain ${domain} has expired!" \ + | ${mailContact} -s "Domain ${domain} has expired!" ${admin} + fi + prints ${domain} "Expired" "${domainDate}" "${domainDiff}" ${registrar} + elif [[ ${domainDiff} -lt ${warnDays} ]] then - if [ "${ALARM}" = "TRUE" ] - then - echo "The domain ${DOMAIN} will expire on ${DOMAINDATE}" \ - | ${MAIL} -s "Domain ${DOMAIN} will expire in ${WARNDAYS}-days or less" ${ADMIN} - fi - prints ${DOMAIN} "Expiring" "${DOMAINDATE}" "${DOMAINDIFF}" "${REGISTRAR}" - else - prints ${DOMAIN} "Valid" "${DOMAINDATE}" "${DOMAINDIFF}" "${REGISTRAR}" - fi + if $alarm + then + echo "The domain ${domain} will expire on ${domainDate}" \ + | ${mailContact} -s "Domain ${domain} will expire in ${warnDays}-days or less" ${admin} + fi + prints ${domain} "Expiring" "${domainDate}" "${registrar}" + else + prints ${domain} "Valid" "${domainDate}" "${domainDiff}" "${registrar}" + fi } #################################################### @@ -423,11 +407,14 @@ #################################################### print_heading() { - if [ "${QUIET}" != "TRUE" ] + if $human + then + if ! $quiet then - printf "\n%-35s %-17s %-8s %-11s %-5s\n" "Domain" "Registrar" "Status" "Expires" "Days Left" - echo "----------------------------------- ----------------- -------- ----------- ---------" + printf "\n%-35s %-17s %-8s %-11s %-5s\n" "Domain" "Registrar" "Status" "Expires" "Days Left" + echo "----------------------------------- ----------------- -------- ----------- ---------" fi + fi } ##################################################################### @@ -441,10 +428,13 @@ ##################################################################### prints() { - if [ "${QUIET}" != "TRUE" ] + if $human then - MIN_DATE=$(echo $3 | ${AWK} '{ print $1, $2, $4 }') - printf "%-35s %-17s %-8s %-11s %-5s\n" "$1" "$5" "$2" "$MIN_DATE" "$4" + if ! $quiet + then + minDate=$(echo $3 | ${awk} '{ print $1, $2, $4 }') + printf "%-35s %-17s %-8s %-11s %-5s\n" "${1:0:35}" "${5:0:17}" "${2:0:8}" "${minDate:0:11}" "${4:0:5}" + fi fi } @@ -455,90 +445,119 @@ ########################################## usage() { - echo "Usage: $0 [ -e email ] [ -x expir_days ] [ -q ] [ -a ] [ -h ]" - echo " {[ -d domain_namee ]} || { -f domainfile}" - echo "" - echo " -a : Send a warning message through email " - echo " -d domain : Domain to analyze (interactive mode)" - echo " -e email address : Email address to send expiration notices" - echo " -f domain file : File with a list of domains" - echo " -h : Print this screen" - echo " -s whois server : Whois sever to query for information" - echo " -q : Don't print anything on the console" - echo " -x days : Domain expiration interval (eg. if domain_date < days)" - echo "" + #app="$(basename $0)" + echo "Usage:" >&2 + echo "Zabbix Agent Mode:" >&2 + echo " [-s whois_host] [-i hostname] <-d domain | -f file>" >&2 + echo "Human Readable Mode:" >&2 + echo " -r [-q][-a] [-e email] [-x days] [-s host] <-d domain|-f file>" >&2 + echo "JSON Mode:" >&2 + echo " -j -f domainfile" >&2 + echo "" >&2 + echo " -a : Send a warning message through email " >&2 + echo " -d domain : Domain to analyze (interactive mode)" >&2 + echo " -e email address : Email address to send expiration notices" >&2 + echo " -f domain file : File with a list of domains" >&2 + echo " -h : Print this screen" >&2 + echo " -i hostname : Hostname for Zabbix Server" >&2 + echo " -j : Discovery JSON Object for Zabbix auto-discovery, (needs -f)" >&2 + echo " -r : Human Readable" >&2 + echo " -s whois server : Whois sever to query for information" >&2 + echo " -q : Don't print anything on the console" >&2 + echo " -x days : Domain expiration interval (eg. if domain_ date < days)" >&2 + echo "" >&2 } ### Evaluate the options passed on the command line -while getopts ae:f:hd:s:qx: option +while getopts ae:f:i:jrhd:s:qx: option do case "${option}" in - a) ALARM="TRUE";; - e) ADMIN=${OPTARG};; - d) DOMAIN=${OPTARG};; - f) SERVERFILE=$OPTARG;; - s) WHOIS_SERVER=$OPTARG;; - q) QUIET="TRUE";; - x) WARNDAYS=$OPTARG;; + a) alarm=true;; + e) admin=${OPTARG};; + d) domain=${OPTARG};; + f) serverFile=$OPTARG;; + i) hostname=$OPTARG;; + j) json=true;; + r) human=true;; + s) whoisServer=$OPTARG;; + q) quiet=true;; + x) warnDays=$OPTARG;; \?) usage exit 1;; esac done ### Check to see if the whois binary exists -if [ ! -f ${WHOIS} ] +if [[ ! -f ${whois} ]] then - echo "ERROR: The whois binary does not exist in ${WHOIS} ." - echo " FIX: Please modify the \$WHOIS variable in the program header." + echo "ERROR: The whois binary does not exist in ${whois}." >&2 + echo " FIX: Please modify the \$whois variable in the program header." >&2 exit 1 fi ### Check to make sure a date utility is available -if [ ! -f ${DATE} ] +if [[ ! -f ${date} ]] then - echo "ERROR: The date binary does not exist in ${DATE} ." - echo " FIX: Please modify the \$DATE variable in the program header." + echo "ERROR: The date binary does not exist in ${date}." >&2 + echo " FIX: Please modify the \$date variable in the program header." >&2 exit 1 fi ### Baseline the dates so we have something to compare to -MONTH=$(${DATE} "+%m") -DAY=$(${DATE} "+%d") -YEAR=$(${DATE} "+%Y") -NOWJULIAN=$(date2julian ${MONTH#0} ${DAY#0} ${YEAR}) - -### Touch the files prior to using them -touch ${WHOIS_TMP} +month=$(${date} "+%m") +DAY=$(${date} "+%d") +YEAR=$(${date} "+%Y") +nowJulian=$(date2julian ${month#0} ${DAY#0} ${YEAR}) ### If a HOST and PORT were passed on the cmdline, use those values -if [ "${DOMAIN}" != "" ] -then - print_heading - check_domain_status "${DOMAIN}" -### If a file and a "-a" are passed on the command line, check all -### of the domains in the file to see if they are about to expire -elif [ -f "${SERVERFILE}" ] +if $json then - print_heading - while read DOMAIN + if [[ -f "${serverFile}" ]] + then + echo "{" + echo " \"data\":[" + echo + while read domain do - sleep 30 - check_domain_status "${DOMAIN}" - - done < ${SERVERFILE} - -### There was an error, so print a detailed usage message and exit -else + echo " { \"{#DOMAIN}\":\"${domain}\" }," + done < ${serverFile} + echo + echo " ]" + echo "}" + else usage exit 1 -fi + fi +else + if [[ "${domain}" != "" ]] + then + print_heading + check_domain_status "${domain}" + ### If a file and a "-a" are passed on the command line, check all + ### of the domains in the file to see if they are about to expire + elif [[ -f "${serverFile}" ]] + then + print_heading + while read domain + do + #sleep 30 + check_domain_status "${domain}" -# Add an extra newline -echo + done < ${serverFile} -### Remove the temporary files -rm -f ${WHOIS_TMP} + ### There was an error, so print a detailed usage message and exit + else + usage + exit 1 + fi + + # Add an extra newline + if $human + then + echo + fi +fi ### Exit with a success indicator exit 0