Guest User

domain-check

a guest
Sep 3rd, 2019
3,575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 16.50 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Program: Domain Expiration Check <domain-check>
  4. #
  5. # Author: Matty < matty91 at gmail dot com >
  6. #
  7. # Current Version: 1.9
  8. #
  9. # Revision History:
  10. #  Version 1.9
  11. #    Bug fix and enhancement for .uk and .co.uk -- Vivek Gite <[email protected]>
  12. #
  13. #  Version 1.8
  14. #    Bug fix added $MAIL -- Vivek Gite <[email protected]>
  15. #
  16. #  Version 1.7
  17. #    Added support for .jp domain names  -- Vivek Gite <[email protected]>
  18. #
  19. #  Version 1.6
  20. #    Added support for .uk domain names; fixed a bug detecting tldtype  -- Vivek Gite <[email protected]>
  21. #
  22. #  Version 1.5
  23. #    Added support for .org, .in, .biz and .info domain names -- Vivek Gite <[email protected]>
  24. #
  25. #  Version 1.4
  26. #    Updated the documentation.
  27. #
  28. #  Version 1.3
  29. #    Gracefully Handle the case where the expiration data is unavailable
  30. #
  31. #  Version 1.2
  32. #    Added "-s" option to allow arbitrary registrars
  33. #
  34. #  Version 1.1
  35. #    Fixed issue with 'e' getopt string -- Pedro Alves
  36. #
  37. #  Version 1.0
  38. #    Initial Release
  39. #
  40. # Last Updated: 07-Aug-2012
  41. #
  42. # Purpose:
  43. #  domain-check checks to see if a domain has expired. domain-check
  44. #  can be run in interactive and batch mode, and provides faciltities
  45. #  to alarm if a domain is about to expire.
  46. #
  47. # License:
  48. #  This program is distributed in the hope that it will be useful,
  49. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  50. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  51. #
  52. # Notes:
  53. #   Since each registrar provides expiration data in a unique format (if
  54. #   they provide it at all), domain-check is currently only able to
  55. #   processess expiration information for a subset of the available
  56. #   registrars.
  57. #
  58. # Requirements:
  59. #   Requires whois
  60. #
  61. # Installation:
  62. #   Copy the shell script to a suitable location
  63. #
  64. # Tested platforms:
  65. #  -- Solaris 9 using /bin/bash
  66. #  -- Solaris 10 using /bin/bash
  67. #  -- OS X 10.4.2 using /bin/sh
  68. #  -- OpenBSD using /bin/sh
  69. #  -- FreeBSD using /bin/sh
  70. #  -- Redhat advanced server 3.0MU3 using /bin/sh
  71. #
  72. # Usage:
  73. #  Refer to the usage() sub-routine, or invoke domain-check
  74. #  with the "-h" option.
  75. #
  76. # Example:
  77. #
  78. #  The first example will print the expiration date and registrar for prefetch.net:
  79. #
  80. #  $ domain-check.sh -d prefetch.net
  81. #
  82. #  Domain                              Registrar         Status   Expires     Days Left
  83. #  ----------------------------------- ----------------- -------- ----------- ---------
  84. #  prefetch.net                        INTERCOSMOS MEDIA Valid    13-feb-2006   64  
  85. #
  86. #  The second example prints the expiration date and registrar for the domains
  87. #  listed in the file "domains":
  88. #
  89. #  $ domain-check.sh -f domains    
  90. #
  91. #  Domain                              Registrar         Status   Expires     Days Left
  92. #  ----------------------------------- ----------------- -------- ----------- ---------
  93. #  sun.com                             NETWORK SOLUTIONS Valid    20-mar-2010   1560
  94. #  google.com                          EMARKMONITOR INC. Valid    14-sep-2011   2103
  95. #  ack.com                             NETWORK SOLUTIONS Valid    09-may-2008   880  
  96. #  prefetch.net                        INTERCOSMOS MEDIA Valid    13-feb-2006   64  
  97. #  spotch.com                          GANDI             Valid    03-dec-2006   357  
  98. #
  99. #  The third example will e-mail the address [email protected] with the domains that
  100. #  will expire in 60-days or less:
  101. #
  102. #  $ domain-check -a -f domains -q -x 60 -e [email protected]  
  103. #
  104.  
  105. PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/ssl/bin:/usr/sfw/bin ; export PATH
  106.  
  107. # Who to page when an expired domain is detected (cmdline: -e)
  108.  
  109. # Mail from:
  110. MFROM="From: Vase podjetje d.o.o. <[email protected]>"
  111.  
  112. # Number of days in the warning threshhold  (cmdline: -x)
  113. WARNDAYS=30
  114.  
  115. # If QUIET is set to TRUE, don't print anything on the console (cmdline: -q)
  116. QUIET="FALSE"
  117.  
  118. # Don't send emails by default (cmdline: -a)
  119. ALARM="FALSE"
  120.  
  121. # Whois server to use (cmdline: -s)
  122. WHOIS_SERVER="whois.internic.org"
  123.  
  124. # Location of system binaries
  125. AWK="/usr/bin/awk"
  126. WHOIS="/usr/bin/whois"
  127. DATE="/bin/date"
  128. CUT="/usr/bin/cut"
  129. MAIL="/usr/bin/mail"
  130. # Place to stash temporary files
  131. WHOIS_TMP="/var/tmp/whois.$$"
  132.  
  133. #############################################################################
  134. # Purpose: Convert a date from MONTH-DAY-YEAR to Julian format
  135. # Acknowledgements: Code was adapted from examples in the book
  136. #                   "Shell Scripting Recipes: A Problem-Solution Approach"
  137. #                   ( ISBN 1590594711 )
  138. # Arguments:
  139. #   $1 -> Month (e.g., 06)
  140. #   $2 -> Day   (e.g., 08)
  141. #   $3 -> Year  (e.g., 2006)
  142. #############################################################################
  143. date2julian()
  144. {
  145.     if [ "${1} != "" ] && [ "${2} != ""  ] && [ "${3}" != "" ]
  146.     then
  147.          ## Since leap years add aday at the end of February,
  148.          ## calculations are done from 1 March 0000 (a fictional year)
  149.          d2j_tmpmonth=$((12 * ${3} + ${1} - 3))
  150.        
  151.           ## If it is not yet March, the year is changed to the previous year
  152.           d2j_tmpyear=$(( ${d2j_tmpmonth} / 12))
  153.        
  154.           ## The number of days from 1 March 0000 is calculated
  155.           ## and the number of days from 1 Jan. 4713BC is added
  156.           echo $(( (734 * ${d2j_tmpmonth} + 15) / 24 -  2 * ${d2j_tmpyear} + ${d2j_tmpyear}/4
  157.                         - ${d2j_tmpyear}/100 + ${d2j_tmpyear}/400 + $2 + 1721119 ))
  158.     else
  159.           echo 0
  160.     fi
  161. }
  162.  
  163. #############################################################################
  164. # Purpose: Convert a string month into an integer representation
  165. # Arguments:
  166. #   $1 -> Month name (e.g., Sep)
  167. #############################################################################
  168. getmonth()
  169. {
  170.        LOWER=`tolower $1`
  171.              
  172.        case ${LOWER} in
  173.              jan) echo 1 ;;
  174.              feb) echo 2 ;;
  175.              mar) echo 3 ;;
  176.              apr) echo 4 ;;
  177.              may) echo 5 ;;
  178.              jun) echo 6 ;;
  179.              jul) echo 7 ;;
  180.              aug) echo 8 ;;
  181.              sep) echo 9 ;;
  182.              oct) echo 10 ;;
  183.              nov) echo 11 ;;
  184.              dec) echo 12 ;;
  185.                *) echo  0 ;;
  186.        esac
  187. }
  188.  
  189. #############################################################################
  190. # Purpose: Calculate the number of seconds between two dates
  191. # Arguments:
  192. #   $1 -> Date #1
  193. #   $2 -> Date #2
  194. #############################################################################
  195. date_diff()
  196. {
  197.         if [ "${1}" != "" ] &&  [ "${2}" != "" ]
  198.         then
  199.                 echo $(expr ${2} - ${1})
  200.         else
  201.                 echo 0
  202.         fi
  203. }
  204.  
  205. ##################################################################
  206. # Purpose: Converts a string to lower case
  207. # Arguments:
  208. #   $1 -> String to convert to lower case
  209. ##################################################################
  210. tolower()
  211. {
  212.      LOWER=`echo ${1} | tr [A-Z] [a-z]`
  213.      echo $LOWER
  214. }
  215.  
  216. ##################################################################
  217. # Purpose: Access whois data to grab the registrar and expiration date
  218. # Arguments:
  219. #   $1 -> Domain to check
  220. ##################################################################
  221. check_domain_status()
  222. {
  223.     local REGISTRAR=""
  224.     # Avoid WHOIS LIMIT EXCEEDED - slowdown our whois client by adding 3 sec
  225.     sleep 7
  226.     # Save the domain since set will trip up the ordering
  227.     DOMAIN=${1}
  228.     TLDTYPE="`echo ${DOMAIN} | cut -d '.' -f3 | tr '[A-Z]' '[a-z]'`"
  229.     if [ "${TLDTYPE}"  == "" ];
  230.     then
  231.         TLDTYPE="`echo ${DOMAIN} | cut -d '.' -f2 | tr '[A-Z]' '[a-z]'`"
  232.     fi
  233.  
  234.     # Invoke whois to find the domain registrar and expiration date
  235.     #${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP}
  236.     # Let whois select server
  237.     if [ "${TLDTYPE}"  == "org" ];
  238.     then
  239.         ${WHOIS} -h "whois.pir.org" "${1}" > ${WHOIS_TMP}
  240.     elif [ "${TLDTYPE}"  == "in" ]; # India
  241.     then
  242.         ${WHOIS} -h "whois.registry.in" "${1}" > ${WHOIS_TMP}
  243.     elif [ "${TLDTYPE}"  == "uk" ]; # United Kingdom  
  244.     then
  245.         ${WHOIS} -h "whois.nic.uk" "${1}" > ${WHOIS_TMP}
  246.     elif [ "${TLDTYPE}"  == "si" ]; # Slovenia
  247.     then
  248.         ${WHOIS} -h "whois.arnes.si" "${1}" > ${WHOIS_TMP}
  249.     elif [ "${TLDTYPE}"  == "biz" ];
  250.     then
  251.         ${WHOIS} -h "whois.neulevel.biz" "${1}" > ${WHOIS_TMP}
  252.     elif [ "${TLDTYPE}"  == "info" ];
  253.     then
  254.         ${WHOIS} -h "whois.afilias.info" "${1}" > ${WHOIS_TMP}
  255.     elif [ "${TLDTYPE}"  == "jp" ]; # Japan
  256.     then
  257.         ${WHOIS} -h "whois.jprs.jp" "${1}" > ${WHOIS_TMP}
  258.  
  259.     elif [ "${TLDTYPE}"  == "com" -o "${TLDTYPE}"  == "net" -o "${TLDTYPE}"  == "edu" ];
  260.     then
  261.     ${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP}
  262.     else
  263.     ${WHOIS} "${1}" > ${WHOIS_TMP}
  264.     fi
  265.  
  266.     # Parse out the expiration date and registrar -- uses the last registrar it finds
  267.     REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar/ && $2 != ""  { REGISTRAR=substr($2,2,17) } END { print REGISTRAR }'`
  268.  
  269.     if [ "${TLDTYPE}" == "uk" ]; # for .uk domain
  270.     then
  271.     REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $0 != ""  { getline; REGISTRAR=substr($0,2,17) } END { print REGISTRAR }'`
  272.     elif [ "${TLDTYPE}" == "si" ]; # for Slovenian domains
  273.     then
  274.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/registrar:/ { print $2 }'`
  275.     elif [ "${TLDTYPE}" == "jp" ];
  276.     then
  277.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/Registrant/ && $2 != ""  { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
  278.  
  279.      fi
  280.  
  281.     # If the Registrar is NULL, then we didn't get any data
  282.     if [ "${REGISTRAR}" = "" ]
  283.     then
  284.         prints "$DOMAIN" "Unknown" "Unknown" "Unknown" "Unknown"
  285.         return
  286.     fi
  287.  
  288.     # The whois Expiration data should resemble the following: "Expiration Date: 09-may-2008"
  289.  
  290.     # for .in, .info, .org domains
  291.     if [ "${TLDTYPE}" == "in" -o "${TLDTYPE}" == "info" -o "${TLDTYPE}" == "org" ];
  292.     then
  293.         DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration Date:/ { print $2 }' | cut -d':' -f2`
  294.     elif [ "${TLDTYPE}" == "biz" ]; # for .biz domain
  295.     then
  296.             DOMAINDATE=`cat ${WHOIS_TMP} | awk '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'`
  297.     elif [ "${TLDTYPE}" == "uk" ]; # for .uk domain
  298.     then
  299.             DOMAINDATE=`cat ${WHOIS_TMP} | awk '/Renewal date:/ || /Expiry date:/ { print $3 }'`
  300.     elif [ "${TLDTYPE}" == "si" ]; # for .si domain
  301.     then
  302.             DOMAINDATE=`cat ${WHOIS_TMP} | awk '/expire:/ { d="date +\"%d-%b-%Y\" -d \""$2"\""; d | getline v; print v; close (d) }'`
  303.     elif [ "${TLDTYPE}" == "jp" ]; # for .jp 2010/04/30
  304.     then
  305.         tdomdate=`cat ${WHOIS_TMP} | awk '/Expires on/ { print $3 }'`
  306.             tyear=`echo ${tdomdate} | cut -d'/' -f1`
  307.             tmon=`echo ${tdomdate} | cut -d'/' -f2`
  308.            case ${tmon} in
  309.                  1|01) tmonth=jan ;;
  310.                  2|02) tmonth=feb ;;
  311.                  3|03) tmonth=mar ;;
  312.                  4|04) tmonth=apr ;;
  313.                  5|05) tmonth=may ;;
  314.                  6|06) tmonth=jun ;;
  315.                  7|07) tmonth=jul ;;
  316.                  8|08) tmonth=aug ;;
  317.                  9|09) tmonth=sep ;;
  318.                  10)tmonth=oct ;;
  319.                  11) tmonth=nov ;;
  320.                  12) tmonth=dec ;;
  321.                       *) tmonth=0 ;;
  322.         esac
  323.             tday=`echo ${tdomdate} | cut -d'/' -f3`
  324.         DOMAINDATE=`echo $tday-$tmonth-$tyear`
  325.     else # .com, .edu, .net and may work with others     
  326.         DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration/ { print $NF }'`
  327.     fi
  328.  
  329.     #echo $DOMAINDATE # debug
  330.     # Whois data should be in the following format: "13-feb-2006"
  331.     IFS="-"
  332.     set -- ${DOMAINDATE}
  333.     MONTH=$(getmonth ${2})
  334.     IFS=""
  335.  
  336.     # Convert the date to seconds, and get the diff between NOW and the expiration date
  337.     DOMAINJULIAN=$(date2julian ${MONTH} ${1#0} ${3})
  338.     DOMAINDIFF=$(date_diff ${NOWJULIAN} ${DOMAINJULIAN})
  339.  
  340.     if [ ${DOMAINDIFF} -lt 0 ]
  341.     then
  342.           if [ "${ALARM}" = "TRUE" ]
  343.           then
  344.                 echo "Domena ${DOMAIN} je potekla!" \
  345.                 | ${MAIL} -s "Domena ${DOMAIN} je potekla!" ${ADMIN}
  346.            fi
  347.  
  348.            prints ${DOMAIN} "Poteklo" "${DOMAINDATE}" "${DOMAINDIFF}" ${REGISTRAR}
  349.  
  350.     elif [ ${DOMAINDIFF} -lt ${WARNDAYS} ]
  351.     then
  352.            if [ "${ALARM}" = "TRUE" ]
  353.            then
  354.                     echo "Domena ${DOMAIN} bo potekla dne ${DOMAINDATE}" \
  355.                     | ${MAIL} -a $MFROM -s "Domena ${DOMAIN} bo potekla v ${WARNDAYS}-dnevih ali manj" ${ADMIN}
  356.             fi
  357.             prints ${DOMAIN} "Hmalu Poteklo" "${DOMAINDATE}" "${DOMAINDIFF}" "${REGISTRAR}"
  358.      else
  359.             prints ${DOMAIN} "Valid" "${DOMAINDATE}"  "${DOMAINDIFF}" "${REGISTRAR}"
  360.      fi
  361. }
  362.  
  363. ####################################################
  364. # Purpose: Print a heading with the relevant columns
  365. # Arguments:
  366. #   None
  367. ####################################################
  368. print_heading()
  369. {
  370.         if [ "${QUIET}" != "TRUE" ]
  371.         then
  372.                 printf "\n%-35s %-17s %-8s %-11s %-5s\n" "Domain" "Registrar" "Status" "Expires" "Days left"
  373.                 echo "----------------------------------- ----------------- -------- ----------- ---------"
  374.         fi
  375. }
  376.  
  377. #####################################################################
  378. # Purpose: Print a line with the expiraton interval
  379. # Arguments:
  380. #   $1 -> Domain
  381. #   $2 -> Status of domain (e.g., expired or valid)
  382. #   $3 -> Date when domain will expire
  383. #   $4 -> Days left until the domain will expire
  384. #   $5 -> Domain registrar
  385. #####################################################################
  386. prints()
  387. {
  388.     if [ "${QUIET}" != "TRUE" ]
  389.     then
  390.             MIN_DATE=$(echo $3 | ${AWK} '{ print $1, $2, $4 }')
  391.             printf "%-35s %-17s %-8s %-11s %-5s\n" "$1" "$5" "$2" "$MIN_DATE" "$4"
  392.     fi
  393. }
  394.  
  395. ##########################################
  396. # Purpose: Describe how the script works
  397. # Arguments:
  398. #   None
  399. ##########################################
  400. usage()
  401. {
  402.         echo "Usage: $0 [ -e email ] [ -x expir_days ] [ -q ] [ -a ] [ -h ]"
  403.         echo "          {[ -d domain_namee ]} || { -f domainfile}"
  404.         echo ""
  405.         echo "  -a               : Send a warning message through email "
  406.         echo "  -d domain        : Domain to analyze (interactive mode)"
  407.         echo "  -e email address : Email address to send expiration notices"
  408.         echo "  -f domain file   : File with a list of domains"
  409.         echo "  -h               : Print this screen"
  410.         echo "  -s whois server  : Whois sever to query for information"
  411.         echo "  -q               : Don't print anything on the console"
  412.         echo "  -x days          : Domain expiration interval (eg. if domain_date < days)"
  413.         echo ""
  414. }
  415.  
  416. ### Evaluate the options passed on the command line
  417. while getopts ae:f:hd:s:qx: option
  418. do
  419.         case "${option}"
  420.         in
  421.                 a) ALARM="TRUE";;
  422.                 e) ADMIN=${OPTARG};;
  423.                 d) DOMAIN=${OPTARG};;
  424.                 f) SERVERFILE=$OPTARG;;
  425.                 s) WHOIS_SERVER=$OPTARG;;
  426.                 q) QUIET="TRUE";;
  427.                 x) WARNDAYS=$OPTARG;;
  428.                 \?) usage
  429.                     exit 1;;
  430.         esac
  431. done
  432.  
  433. ### Check to see if the whois binary exists
  434. if [ ! -f ${WHOIS} ]
  435. then
  436.         echo "ERROR: The whois binary does not exist in ${WHOIS} ."
  437.         echo "  FIX: Please modify the \$WHOIS variable in the program header."
  438.         exit 1
  439. fi
  440.  
  441. ### Check to make sure a date utility is available
  442. if [ ! -f ${DATE} ]
  443. then
  444.         echo "ERROR: The date binary does not exist in ${DATE} ."
  445.         echo "  FIX: Please modify the \$DATE variable in the program header."
  446.         exit 1
  447. fi
  448.  
  449. ### Baseline the dates so we have something to compare to
  450. MONTH=$(${DATE} "+%m")
  451. DAY=$(${DATE} "+%d")
  452. YEAR=$(${DATE} "+%Y")
  453. NOWJULIAN=$(date2julian ${MONTH#0} ${DAY#0} ${YEAR})
  454.  
  455. ### Touch the files prior to using them
  456. touch ${WHOIS_TMP}
  457.  
  458. ### If a HOST and PORT were passed on the cmdline, use those values
  459. if [ "${DOMAIN}" != "" ]
  460. then
  461.         print_heading
  462.         check_domain_status "${DOMAIN}"
  463. ### If a file and a "-a" are passed on the command line, check all
  464. ### of the domains in the file to see if they are about to expire
  465. elif [ -f "${SERVERFILE}" ]
  466. then
  467.         print_heading
  468.         while read DOMAIN
  469.         do
  470.                 check_domain_status "${DOMAIN}"
  471.  
  472.         done < ${SERVERFILE}
  473.  
  474. ### There was an error, so print a detailed usage message and exit
  475. else
  476.         usage
  477.         exit 1
  478. fi
  479.  
  480. # Add an extra newline
  481. echo
  482.  
  483. ### Remove the temporary files
  484. rm -f ${WHOIS_TMP}
  485.  
  486. ### Exit with a success indicator
  487. exit 0
Advertisement
Add Comment
Please, Sign In to add comment