BASH   79

Domain Expiration Check

Guest on 7th June 2022 01:31:38 AM

  1. #!/usr/bin/env bash
  2.  
  3. #
  4. # Program: Domain Expiration Check <domain-check>
  5. #
  6. # Author: Matty < matty91 at gmail dot com >
  7. #
  8. # Current Version: 2.10
  9. #
  10. # Revision History:
  11. #
  12. #  Version 2.10
  13. #   Bug fix for .com, net, org, jp, and edu  -- Vivek Gite
  14. #
  15. #  Version 2.9
  16. #   Bug fix for .ru -- Mikhail Grigorev
  17. #
  18. #  Version 2.8
  19. #   Use 'which' to autodetect awk, whois, date and other utils -- Mikhail Grigorev
  20. #
  21. #  Version 2.7
  22. #   Bug fix for .md and .co -- Bill Bell
  23. #
  24. #  Version 2.6
  25. #   Bug fix for .mobi -- Bill Bell
  26. #
  27. #  Version 2.5
  28. #   Bug fix for .me -- Mikhail Grigorev
  29. #
  30. #  Version 2.4
  31. #   Bug fix for .com, .net, .us, .org, .in -- Mikhail Grigorev
  32. #
  33. #  Version 2.3
  34. #   Bug fix for .info -- Mikhail Grigorev
  35. #
  36. #  Version 2.2
  37. #   Bug fix that adds support for .ru and .su domains -- Jim McNamara
  38. #
  39. #  Version 2.1
  40. #   Bug fix for .mobi and .us -- Jim McNamara
  41. #   Added a variable for the location of tr, and made all calls to cut and tr use the variable
  42. #   instead of the command without path
  43. #
  44. #  Version 2.0
  45. #   Bug fix for .org, .biz, info, and .ca -- Cameron and Jim
  46. #
  47. #  Version 1.9
  48. #    Bug fix and enhancement for .uk and .co.uk -- Vivek Gite
  49. #
  50. #  Version 1.8
  51. #    Bug fix added $MAIL -- Vivek Gite
  52. #
  53. #  Version 1.7
  54. #    Added support for .jp domain names  -- Vivek Gite
  55. #
  56. #  Version 1.6
  57. #    Added support for .uk domain names; fixed a bug detecting tldtype  -- Vivek Gite
  58. #
  59. #  Version 1.5
  60. #    Added support for .org, .in, .biz and .info domain names -- Vivek Gite
  61. #
  62. #  Version 1.4
  63. #    Updated the documentation.
  64. #
  65. #  Version 1.3
  66. #    Gracefully Handle the case where the expiration data is unavailable
  67. #
  68. #  Version 1.2
  69. #    Added "-s" option to allow arbitrary registrars
  70. #
  71. #  Version 1.1
  72. #    Fixed issue with 'e' getopt string -- Pedro Alves
  73. #
  74. #  Version 1.0
  75. #    Initial Release
  76. #
  77. # Last Updated: 13-Apr-2016
  78. #
  79. # Purpose:
  80. #  domain-check checks to see if a domain has expired. domain-check
  81. #  can be run in interactive and batch mode, and provides faciltities
  82. #  to alarm if a domain is about to expire.
  83. #
  84. # License:
  85. #  This program is distributed in the hope that it will be useful,
  86. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  87. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  88. #
  89. # Notes:
  90. #   Since each registrar provides expiration data in a unique format (if
  91. #   they provide it at all), domain-check is currently only able to
  92. #   processess expiration information for a subset of the available
  93. #   registrars.
  94. #
  95. # Requirements:
  96. #   Requires whois
  97. #
  98. # Installation:
  99. #   Copy the shell script to a suitable location
  100. #
  101. # Tested platforms:
  102. #  -- Solaris 9 using /bin/bash
  103. #  -- Solaris 10 using /bin/bash
  104. #  -- OS X 10.4.2 using /bin/sh
  105. #  -- OpenBSD using /bin/sh
  106. #  -- FreeBSD using /bin/sh
  107. #  -- Redhat advanced server 3.0MU3 using /bin/sh
  108. #
  109. # Usage:
  110. #  Refer to the usage() sub-routine, or invoke domain-check
  111. #  with the "-h" option.
  112. #
  113. # Example:
  114. #
  115. #  The first example will print the expiration date and registrar for prefetch.net:
  116. #
  117. #  $ domain-check.sh -d prefetch.net
  118. #
  119. #  Domain                              Registrar         Status   Expires     Days Left
  120. #  ----------------------------------- ----------------- -------- ----------- ---------
  121. #  prefetch.net                        INTERCOSMOS MEDIA Valid    13-feb-2006   64  
  122. #
  123. #  The second example prints the expiration date and registrar for the domains
  124. #  listed in the file "domains":
  125. #
  126. #  $ domain-check.sh -f domains    
  127. #
  128. #  Domain                              Registrar         Status   Expires     Days Left
  129. #  ----------------------------------- ----------------- -------- ----------- ---------
  130. #  sun.com                             NETWORK SOLUTIONS Valid    20-mar-2010   1560
  131. #  google.com                          EMARKMONITOR INC. Valid    14-sep-2011   2103
  132. #  ack.com                             NETWORK SOLUTIONS Valid    09-may-2008   880  
  133. #  prefetch.net                        INTERCOSMOS MEDIA Valid    13-feb-2006   64  
  134. #  spotch.com                          GANDI             Valid    03-dec-2006   357  
  135. #
  136. #  The third example will e-mail the address admin@prefetch.net with the domains that
  137. #  will expire in 60-days or less:
  138. #
  139. #  $ domain-check -a -f domains -q -x 60 -e admin@prefetch.net  
  140. #
  141.  
  142. PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/ssl/bin:/usr/sfw/bin
  143. export PATH
  144.  
  145. # Who to page when an expired domain is detected (cmdline: -e)
  146. ADMIN="sysadmin@mydomain.com"
  147.  
  148. # Number of days in the warning threshhold  (cmdline: -x)
  149. WARNDAYS=30
  150.  
  151. # If QUIET is set to TRUE, don't print anything on the console (cmdline: -q)
  152. QUIET="FALSE"
  153.  
  154. # Don't send emails by default (cmdline: -a)
  155. ALARM="FALSE"
  156.  
  157. # Whois server to use (cmdline: -s)
  158. WHOIS_SERVER="whois.internic.org"
  159.  
  160. # Location of system binaries
  161. AWK=`which awk`
  162. WHOIS=`which whois`
  163. DATE=`which date`
  164. CUT=`which cut`
  165. TR=`which tr`
  166. MAIL=`which mail`
  167.  
  168. # Place to stash temporary files
  169. WHOIS_TMP="/var/tmp/whois.$$"
  170.  
  171. #############################################################################
  172. # Purpose: Convert a date from MONTH-DAY-YEAR to Julian format
  173. # Acknowledgements: Code was adapted from examples in the book
  174. #                   "Shell Scripting Recipes: A Problem-Solution Approach"
  175. #                   ( ISBN 1590594711 )
  176. # Arguments:
  177. #   $1 -> Month (e.g., 06)
  178. #   $2 -> Day   (e.g., 08)
  179. #   $3 -> Year  (e.g., 2006)
  180. #############################################################################
  181. date2julian()
  182. {
  183.     if [ "${1} != "" ] && [ "${2} != ""  ] && [ "${3}" != "" ]
  184.     then
  185.          ## Since leap years add aday at the end of February,
  186.          ## calculations are done from 1 March 0000 (a fictional year)
  187.          d2j_tmpmonth=$((12 * ${3} + ${1} - 3))
  188.        
  189.          ## If it is not yet March, the year is changed to the previous year
  190.          d2j_tmpyear=$(( ${d2j_tmpmonth} / 12))
  191.        
  192.          ## The number of days from 1 March 0000 is calculated
  193.          ## and the number of days from 1 Jan. 4713BC is added
  194.          echo $(( (734 * ${d2j_tmpmonth} + 15) / 24 -  2 * ${d2j_tmpyear} + ${d2j_tmpyear}/4
  195.                        - ${d2j_tmpyear}/100 + ${d2j_tmpyear}/400 + $2 + 1721119 ))
  196.     else
  197.          echo 0
  198.     fi
  199. }
  200.  
  201. #############################################################################
  202. # Purpose: Convert a string month into an integer representation
  203. # Arguments:
  204. #   $1 -> Month name (e.g., Sep)
  205. #############################################################################
  206. getmonth()
  207. {
  208.        LOWER=`tolower $1`
  209.              
  210.        case ${LOWER} in
  211.              jan) echo 1 ;;
  212.              feb) echo 2 ;;
  213.              mar) echo 3 ;;
  214.              apr) echo 4 ;;
  215.              may) echo 5 ;;
  216.              jun) echo 6 ;;
  217.              jul) echo 7 ;;
  218.              aug) echo 8 ;;
  219.              sep) echo 9 ;;
  220.              oct) echo 10 ;;
  221.              nov) echo 11 ;;
  222.              dec) echo 12 ;;
  223.                *) echo  0 ;;
  224.        esac
  225. }
  226.  
  227. #############################################################################
  228. # Purpose: Calculate the number of seconds between two dates
  229. # Arguments:
  230. #   $1 -> Date #1
  231. #   $2 -> Date #2
  232. #############################################################################
  233. date_diff()
  234. {
  235.         if [ "${1}" != "" ] &&  [ "${2}" != "" ]
  236.         then
  237.                 echo $(expr ${2} - ${1})
  238.         else
  239.                 echo 0
  240.         fi
  241. }
  242.  
  243. ##################################################################
  244. # Purpose: Converts a string to lower case
  245. # Arguments:
  246. #   $1 -> String to convert to lower case
  247. ##################################################################
  248. tolower()
  249. {
  250.      LOWER=`echo ${1} | ${TR} [A-Z] [a-z]`
  251.      echo $LOWER
  252. }
  253.  
  254. ##################################################################
  255. # Purpose: Access whois data to grab the registrar and expiration date
  256. # Arguments:
  257. #   $1 -> Domain to check
  258. ##################################################################
  259. check_domain_status()
  260. {
  261.     local REGISTRAR=""
  262.     # Avoid WHOIS LIMIT EXCEEDED - slowdown our whois client by adding 3 sec
  263.     sleep 3
  264.     # Save the domain since set will trip up the ordering
  265.     DOMAIN=${1}
  266.     TLDTYPE="`echo ${DOMAIN} | ${CUT} -d '.' -f3 | tr '[A-Z]' '[a-z]'`"
  267.     if [ "${TLDTYPE}"  == "" ];
  268.     then
  269.             TLDTYPE="`echo ${DOMAIN} | ${CUT} -d '.' -f2 | tr '[A-Z]' '[a-z]'`"
  270.     fi
  271.  
  272.     # Invoke whois to find the domain registrar and expiration date
  273.     #${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP}
  274.     # Let whois select server
  275.     if [ "${TLDTYPE}"  == "org" ];
  276.     then
  277.         ${WHOIS} -h "whois.pir.org" "${1}" > ${WHOIS_TMP}
  278.     elif [ "${TLDTYPE}"  == "in" ]; # India
  279.     then
  280.         ${WHOIS} -h "whois.registry.in" "${1}" > ${WHOIS_TMP}
  281.     elif [ "${TLDTYPE}"  == "co" ];
  282.     then
  283.         ${WHOIS} -h "whois.nic.co" "${1}" > ${WHOIS_TMP}
  284.     elif [ "${TLDTYPE}"  == "md" ];
  285.     then
  286.         ${WHOIS} -h "whois.nic.md" "${1}" > ${WHOIS_TMP}
  287.     elif [ "${TLDTYPE}"  == "uk" ]; # United Kingdom  
  288.     then
  289.         ${WHOIS} -h "whois.nic.uk" "${1}" > ${WHOIS_TMP}
  290.  
  291.     elif [ "${TLDTYPE}"  == "biz" ];
  292.     then
  293.         ${WHOIS} -h "whois.neulevel.biz" "${1}" > ${WHOIS_TMP}
  294.     elif [ "${TLDTYPE}"  == "info" ];
  295.     then
  296.         ${WHOIS} -h "whois.afilias.info" "${1}" > ${WHOIS_TMP}
  297.     elif [ "${TLDTYPE}"  == "tv" ];
  298.     then
  299.         ${WHOIS} -h "tvwhois.verisign-grs.com" "${1}" > ${WHOIS_TMP}
  300.     elif [ "${TLDTYPE}"  == "jp" ]; # Japan
  301.     then
  302.         ${WHOIS} -h "whois.jprs.jp" "${1}/e" > ${WHOIS_TMP}
  303.  
  304.     elif [ "${TLDTYPE}"  == "ca" ]; # Canada
  305.     then
  306.         ${WHOIS} -h "whois.cira.ca" "${1}" > ${WHOIS_TMP}
  307.     elif [ "${TLDTYPE}"  == "edu" ]; # edu added by nixCraft on 26-aug-2017
  308.     then
  309.         ${WHOIS} -h "whois.educause.edu" "${1}" > ${WHOIS_TMP}
  310.  
  311.     elif [ "${TLDTYPE}"  == "com" -o "${TLDTYPE}"  == "net" ];
  312.     then
  313.         ${WHOIS} -h ${WHOIS_SERVER} "=${1}" > ${WHOIS_TMP}
  314.     elif [ "${TLDTYPE}" == "ru" -o "${TLDTYPE}" == "su" ]; # Russia and Soviet Union added 20141113
  315.     then
  316.         ${WHOIS} -h "whois.ripn.net" "${1}" > ${WHOIS_TMP}
  317.     else
  318.         ${WHOIS} "${1}" > ${WHOIS_TMP}
  319.     fi
  320.  
  321.     # Parse out the expiration date and registrar -- uses the last registrar it finds
  322.     REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $2 != ""  { REGISTRAR=substr($2,2,17) } END { print REGISTRAR }'`
  323.  
  324.     if [ "${TLDTYPE}" == "uk" ]; # for .uk domain
  325.     then
  326.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $0 != ""  { getline; REGISTRAR=substr($0,2,17) } END { print REGISTRAR }'`
  327.     elif [ "${TLDTYPE}" == "me" ];
  328.     then
  329.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $2 != ""  { REGISTRAR=substr($2,2,23) } END { print REGISTRAR }'`
  330.     elif [ "${TLDTYPE}" == "jp" ];
  331.     then
  332.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} '/Registrant/ && $2 != ""  { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
  333.     elif [ "${TLDTYPE}" == "in" ];
  334.     then
  335.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Sponsoring Registrar:/ && $2 != ""  { REGISTRAR=substr($2,1,47) } END { print REGISTRAR }'`
  336.     elif [ "${TLDTYPE}" == "md" ];
  337.     then
  338.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrant:/ && $2 != ""  { REGISTRAR=substr($2,1,47) } END { print REGISTRAR }'`
  339.     # No longer needed for .org. Commented out by nixCraft on 26-aug-2017
  340.     #elif [ "${TLDTYPE}" == "org" ];
  341.     #then
  342.     #    REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Sponsoring Registrar:/ && $2 != ""  { REGISTRAR=substr($2,2,17) } END { print REGISTRAR }'`
  343.     elif [ "${TLDTYPE}" == "info" ];
  344.     then
  345.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $2 != ""  { REGISTRAR=substr($2,2,17) } END { print REGISTRAR }'`
  346.     elif [ "${TLDTYPE}" == "biz" -o "${TLDTYPE}" == "co" ];
  347.     then
  348.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $2 != ""  { REGISTRAR=substr($2,20,17) } END { print REGISTRAR }'`
  349.     elif [ "${TLDTYPE}" == "ca" ];
  350.     then
  351.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrar:/ && $0 != ""  { getline; REGISTRAR=substr($0,24,17) } END { print REGISTRAR }'`
  352.     elif [ "${TLDTYPE}" == "mobi" ];
  353.     then
  354.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Updated by Registrar:/ && $2 != "" { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
  355.         if [ "${REGISTRAR}" = "" ]
  356.         then
  357.                 REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Sponsoring Registrar:/ && $2 != "" { REGISTRAR=substr($2,1,17) } END { print REGISTRAR }'`
  358.         fi
  359.     elif [ "${TLDTYPE}" == "us" ];
  360.     then
  361.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Sponsoring Registrar:/ && $2 != ""  { REGISTRAR=substr($2,25,17) } END { print REGISTRAR }'`
  362.     elif [ "${TLDTYPE}" == "edu" ]; # added by nixCraft 26-aug-2017
  363.     then
  364.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/Registrant:/ && $0 != ""  { getline;REGISTRAR=substr($0,1,17) } END { print REGISTRAR }'`
  365.     elif [ "${TLDTYPE}" == "ru" -o "${TLDTYPE}" == "su" ]; # added 20141113
  366.     then
  367.         REGISTRAR=`cat ${WHOIS_TMP} | ${AWK} -F: '/registrar:/ && $2 != "" { REGISTRAR=substr($2,6,17) } END { print REGISTRAR }'`
  368.     fi
  369.  
  370.     # If the Registrar is NULL, then we didn't get any data
  371.     if [ "${REGISTRAR}" = "" ]
  372.     then
  373.         prints "$DOMAIN" "Unknown" "Unknown" "Unknown" "Unknown"
  374.         return
  375.     fi
  376.  
  377.     # The whois Expiration data should resemble the following: "Expiration Date: 09-may-2008"
  378.  
  379.     if [ "${TLDTYPE}" == "in" ];
  380.     then
  381.         DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration Date:/ { print $2 }' | ${CUT} -d ':' -f2`
  382.     elif [ "${TLDTYPE}" == "info" -o "${TLDTYPE}" == "org" ];
  383.     then
  384.             tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expiry Date:/ { print $4 }'`
  385.             tyear=`echo ${tdomdate} | ${CUT} -d'-' -f1`
  386.             tmon=`echo ${tdomdate} | ${CUT} -d'-' -f2`
  387.                case ${tmon} in
  388.                      1|01) tmonth=jan ;;
  389.                      2|02) tmonth=feb ;;
  390.                      3|03) tmonth=mar ;;
  391.                      4|04) tmonth=apr ;;
  392.                      5|05) tmonth=may ;;
  393.                      6|06) tmonth=jun ;;
  394.                      7|07) tmonth=jul ;;
  395.                      8|08) tmonth=aug ;;
  396.                      9|09) tmonth=sep ;;
  397.                      10) tmonth=oct ;;
  398.                      11) tmonth=nov ;;
  399.                      12) tmonth=dec ;;
  400.                       *) tmonth=0 ;;
  401.                 esac
  402.             tday=`echo ${tdomdate} | ${CUT} -d'-' -f3 | ${CUT} -d'T' -f1`
  403.             DOMAINDATE=`echo $tday-$tmonth-$tyear`
  404.     elif [ "${TLDTYPE}" == "biz" -o "${TLDTYPE}" == "co" ]; # for .biz and .co domain
  405.     then
  406.             DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Domain Expiration Date:/ { print $6"-"$5"-"$9 }'`
  407.     elif [ "${TLDTYPE}" == "md" ]; # for .md domain
  408.     then
  409.             tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expiration date:/ { print $3 }'`
  410.             tyear=`echo ${tdomdate} | ${CUT} -d'-' -f1`
  411.             tmon=`echo ${tdomdate} | ${CUT} -d'-' -f2`
  412.                case ${tmon} in
  413.                      1|01) tmonth=jan ;;
  414.                      2|02) tmonth=feb ;;
  415.                      3|03) tmonth=mar ;;
  416.                      4|04) tmonth=apr ;;
  417.                      5|05) tmonth=may ;;
  418.                      6|06) tmonth=jun ;;
  419.                      7|07) tmonth=jul ;;
  420.                      8|08) tmonth=aug ;;
  421.                      9|09) tmonth=sep ;;
  422.                      10) tmonth=oct ;;
  423.                      11) tmonth=nov ;;
  424.                      12) tmonth=dec ;;
  425.                       *) tmonth=0 ;;
  426.                 esac
  427.             tday=`echo ${tdomdate} | ${CUT} -d'-' -f3`
  428.             DOMAINDATE=`echo $tday-$tmonth-$tyear`
  429.     elif [ "${TLDTYPE}" == "uk" ]; # for .uk domain
  430.     then
  431.             DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Renewal date:/ || /Expiry date:/ { print $3 }'`
  432.     elif [ "${TLDTYPE}" == "jp" ]; # for .jp 2010/04/30
  433.     then
  434.             tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expires on/ { print $3 }'`
  435.             tyear=`echo ${tdomdate} | ${CUT} -d'/' -f1`
  436.             tmon=`echo ${tdomdate} | ${CUT} -d'/' -f2`
  437.                case ${tmon} in
  438.                      1|01) tmonth=jan ;;
  439.                      2|02) tmonth=feb ;;
  440.                      3|03) tmonth=mar ;;
  441.                      4|04) tmonth=apr ;;
  442.                      5|05) tmonth=may ;;
  443.                      6|06) tmonth=jun ;;
  444.                      7|07) tmonth=jul ;;
  445.                      8|08) tmonth=aug ;;
  446.                      9|09) tmonth=sep ;;
  447.                      10) tmonth=oct ;;
  448.                      11) tmonth=nov ;;
  449.                      12) tmonth=dec ;;
  450.                       *) tmonth=0 ;;
  451.                 esac
  452.             tday=`echo ${tdomdate} | ${CUT} -d'/' -f3`
  453.             DOMAINDATE=`echo $tday-$tmonth-$tyear`
  454.     elif [ "${TLDTYPE}" == "ca" ]; # for .ca 2010/04/30
  455.     then
  456.             tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expiry date/ { print $3 }'`
  457.             tyear=`echo ${tdomdate} | ${CUT} -d'/' -f1`
  458.             tmon=`echo ${tdomdate} | ${CUT} -d'/' -f2`
  459.                case ${tmon} in
  460.                      1|01) tmonth=jan ;;
  461.                      2|02) tmonth=feb ;;
  462.                      3|03) tmonth=mar ;;
  463.                      4|04) tmonth=apr ;;
  464.                      5|05) tmonth=may ;;
  465.                      6|06) tmonth=jun ;;
  466.                      7|07) tmonth=jul ;;
  467.                      8|08) tmonth=aug ;;
  468.                      9|09) tmonth=sep ;;
  469.                      10) tmonth=oct ;;
  470.                      11) tmonth=nov ;;
  471.                      12) tmonth=dec ;;
  472.                       *) tmonth=0 ;;
  473.                 esac
  474.             tday=`echo ${tdomdate} | ${CUT} -d'/' -f3`
  475.             DOMAINDATE=`echo $tday-$tmonth-$tyear`
  476.     elif [ "${TLDTYPE}" == "mobi" -o "${TLDTYPE}" == "tv" ]; # for .mobi and .tv
  477.     then
  478.             if [ "${tdomdate}" = "" ]
  479.             then
  480.                 tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Registry Expiry Date:/ { print $4 }'`
  481.                 tyear=`echo ${tdomdate} | ${CUT} -d "-" -f 1`
  482.                 tmon=`echo ${tdomdate} | ${CUT} -d "-" -f 2`
  483.                   case ${tmon} in
  484.                      1|01) tmonth=jan ;;
  485.                      2|02) tmonth=feb ;;
  486.                      3|03) tmonth=mar ;;
  487.                      4|04) tmonth=apr ;;
  488.                      5|05) tmonth=may ;;
  489.                      6|06) tmonth=jun ;;
  490.                      7|07) tmonth=jul ;;
  491.                      8|08) tmonth=aug ;;
  492.                      9|09) tmonth=sep ;;
  493.                      10) tmonth=oct ;;
  494.                      11) tmonth=nov ;;
  495.                      12) tmonth=dec ;;
  496.                      *) tmonth=0 ;;
  497.                  esac
  498.                 tday=`echo ${tdomdate} | ${CUT} -d "-" -f 3 | ${CUT} -d "T" -f 1`
  499.                 DOMAINDATE=`echo "${tday}-${tmonth}-${tyear}"`
  500.             else
  501.                 tyear=`echo ${tdomdate} | ${CUT} -d'-' -f3`
  502.                 tmon=`echo ${tdomdate} |${CUT} -d'-' -f2`
  503.                 tmonth=`tolower ${tmon}`
  504.                 tday=`echo ${tdomdate} | ${CUT} -d'-' -f1`
  505.                 DOMAINDATE=`echo "${tday}-${tmonth}-${tyear}"`
  506.             fi
  507.     elif [ "${TLDTYPE}" == "us" ]; # for .us 2014/08/11
  508.     then
  509.             tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Expiration Date:/' |${CUT} -d ' ' -f26-`
  510.             tyear=`echo ${tdomdate} | ${CUT} -d' ' -f5`
  511.             tmon=`echo ${tdomdate} |${CUT} -d' ' -f1`
  512.             tmonth=`tolower ${tmon}`
  513.             tday=`echo ${tdomdate} | ${CUT} -d' ' -f2`
  514.             DOMAINDATE=`echo "${tday}-${tmonth}-${tyear}"`
  515.     elif [ "${TLDTYPE}" == "me" ]; # for .me domain
  516.     then
  517.         tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Registry Expiry Date:/ { print $4 }'`
  518.         tyear=`echo ${tdomdate} | ${CUT} -d "-" -f 1`
  519.         tmon=`echo ${tdomdate} | ${CUT} -d "-" -f 2`
  520.                case ${tmon} in
  521.                      1|01) tmonth=jan ;;
  522.                      2|02) tmonth=feb ;;
  523.                      3|03) tmonth=mar ;;
  524.                      4|04) tmonth=apr ;;
  525.                      5|05) tmonth=may ;;
  526.                      6|06) tmonth=jun ;;
  527.                      7|07) tmonth=jul ;;
  528.                      8|08) tmonth=aug ;;
  529.                      9|09) tmonth=sep ;;
  530.                      10) tmonth=oct ;;
  531.                      11) tmonth=nov ;;
  532.                      12) tmonth=dec ;;
  533.                      *) tmonth=0 ;;
  534.                esac
  535.         tday=`echo ${tdomdate} | ${CUT} -d "-" -f 3 | ${CUT} -d "T" -f 1`
  536.         DOMAINDATE=`echo "${tday}-${tmonth}-${tyear}"`
  537.     elif [ "${TLDTYPE}" == "ru" -o "${TLDTYPE}" == "su" ]; # for .ru and .su 2014/11/13
  538.     then
  539.            tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/paid-till:/ { print $2 }'`
  540.            tyear=`echo ${tdomdate} | ${CUT} -d'-' -f1`
  541.            tmon=`echo ${tdomdate} |${CUT} -d'-' -f2`
  542.                case ${tmon} in
  543.                      1|01) tmonth=jan ;;
  544.                      2|02) tmonth=feb ;;
  545.                      3|03) tmonth=mar ;;
  546.                      4|04) tmonth=apr ;;
  547.                      5|05) tmonth=may ;;
  548.                      6|06) tmonth=jun ;;
  549.                      7|07) tmonth=jul ;;
  550.                      8|08) tmonth=aug ;;
  551.                      9|09) tmonth=sep ;;
  552.                      10) tmonth=oct ;;
  553.                      11) tmonth=nov ;;
  554.                      12) tmonth=dec ;;
  555.                      *) tmonth=0 ;;
  556.                esac
  557.            tday=`echo ${tdomdate} | ${CUT} -d "-" -f 3 | ${CUT} -d "T" -f 1`
  558.            DOMAINDATE=`echo "${tday}-${tmonth}-${tyear}"`
  559.     elif [ "${TLDTYPE}" == "com" -o "${TLDTYPE}" == "net" -o "${TLDTYPE}" == "org" ]; # added on 26-aug-2017 by nixCraft {https://www.cyberciti.biz/}
  560.     then
  561.            tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Registry Expiry Date:/ { print $NF }'`
  562.            tyear=`echo ${tdomdate} | ${CUT} -d'-' -f1`
  563.            tmon=`echo ${tdomdate} |${CUT} -d'-' -f2`
  564.                case ${tmon} in
  565.                      1|01) tmonth=jan ;;
  566.                      2|02) tmonth=feb ;;
  567.                      3|03) tmonth=mar ;;
  568.                      4|04) tmonth=apr ;;
  569.                      5|05) tmonth=may ;;
  570.                      6|06) tmonth=jun ;;
  571.                      7|07) tmonth=jul ;;
  572.                      8|08) tmonth=aug ;;
  573.                      9|09) tmonth=sep ;;
  574.                      10) tmonth=oct ;;
  575.                      11) tmonth=nov ;;
  576.                      12) tmonth=dec ;;
  577.                      *) tmonth=0 ;;
  578.                esac
  579.            tday=`echo ${tdomdate} | ${CUT} -d "-" -f 3 | ${CUT} -d "T" -f 1`
  580.            DOMAINDATE=`echo "${tday}-${tmonth}-${tyear}"`
  581.     elif [ "${TLDTYPE}" == "edu" ] # added on 26-aug-2017 by nixCraft
  582.     then
  583.            tdomdate=`cat ${WHOIS_TMP} | ${AWK} '/Domain expires:/ { print $NF }'`
  584.            echo $tomdate
  585.            tyear=`echo ${tdomdate} | ${CUT} -d'-' -f3`
  586.            tmon=`echo ${tdomdate} |${CUT} -d'-' -f2`
  587.                case ${tmon} in
  588.                      1|01) tmonth=jan ;;
  589.                      2|02) tmonth=feb ;;
  590.                      3|03) tmonth=mar ;;
  591.                      4|04) tmonth=apr ;;
  592.                      5|05) tmonth=may ;;
  593.                      6|06) tmonth=jun ;;
  594.                      7|07) tmonth=jul ;;
  595.                      8|08) tmonth=aug ;;
  596.                      9|09) tmonth=sep ;;
  597.                      10) tmonth=oct ;;
  598.                      11) tmonth=nov ;;
  599.                      12) tmonth=dec ;;
  600.                      *) tmonth=0 ;;
  601.                esac
  602.            tday=`echo ${tdomdate} | ${CUT} -d "-" -f 1`
  603.            DOMAINDATE=`echo "${tday}-${tmon}-${tyear}"`
  604.     else # may work with others  ??? ;)  
  605.            DOMAINDATE=`cat ${WHOIS_TMP} | ${AWK} '/Expiration/ { print $NF }'` 
  606.     fi
  607.  
  608.     #echo $DOMAINDATE # debug
  609.     # Whois data should be in the following format: "13-feb-2006"
  610.     IFS="-"
  611.     set -- ${DOMAINDATE}
  612.     MONTH=$(getmonth ${2})
  613.     IFS=""
  614.  
  615.     # Convert the date to seconds, and get the diff between NOW and the expiration date
  616.     DOMAINJULIAN=$(date2julian ${MONTH} ${1#0} ${3})
  617.     DOMAINDIFF=$(date_diff ${NOWJULIAN} ${DOMAINJULIAN})
  618.  
  619.     if [ ${DOMAINDIFF} -lt 0 ]
  620.     then
  621.           if [ "${ALARM}" = "TRUE" ]
  622.           then
  623.                 echo "The domain ${DOMAIN} has expired!" \
  624.                 | ${MAIL} -s "Domain ${DOMAIN} has expired!" ${ADMIN}
  625.            fi
  626.  
  627.            prints "${DOMAIN}" "Expired" "${DOMAINDATE}" "${DOMAINDIFF}" "${REGISTRAR}"
  628.  
  629.     elif [ ${DOMAINDIFF} -lt ${WARNDAYS} ]
  630.     then
  631.            if [ "${ALARM}" = "TRUE" ]
  632.            then
  633.                     echo "The domain ${DOMAIN} will expire on ${DOMAINDATE}" \
  634.                     | ${MAIL} -s "Domain ${DOMAIN} will expire in ${WARNDAYS}-days or less" ${ADMIN}
  635.             fi
  636.             prints "${DOMAIN}" "Expiring" "${DOMAINDATE}" "${DOMAINDIFF}" "${REGISTRAR}"
  637.      else
  638.             prints "${DOMAIN}" "Valid" "${DOMAINDATE}"  "${DOMAINDIFF}" "${REGISTRAR}"
  639.      fi
  640. }
  641.  
  642. ####################################################
  643. # Purpose: Print a heading with the relevant columns
  644. # Arguments:
  645. #   None
  646. ####################################################
  647. print_heading()
  648. {
  649.         if [ "${QUIET}" != "TRUE" ]
  650.         then
  651.                 printf "\n%-35s %-46s %-8s %-11s %-5s\n" "Domain" "Registrar" "Status" "Expires" "Days Left"
  652.                 echo "----------------------------------- ---------------------------------------------- -------- ----------- ---------"
  653.         fi
  654. }
  655.  
  656. #####################################################################
  657. # Purpose: Print a line with the expiraton interval
  658. # Arguments:
  659. #   $1 -> Domain
  660. #   $2 -> Status of domain (e.g., expired or valid)
  661. #   $3 -> Date when domain will expire
  662. #   $4 -> Days left until the domain will expire
  663. #   $5 -> Domain registrar
  664. #####################################################################
  665. prints()
  666. {
  667.     if [ "${QUIET}" != "TRUE" ]
  668.     then
  669.             MIN_DATE=$(echo $3 | ${AWK} '{ print $1, $2, $4 }')
  670.             printf "%-35s %-46s %-8s %-11s %-5s\n" "$1" "$5" "$2" "$MIN_DATE" "$4"
  671.     fi
  672. }
  673.  
  674. ##########################################
  675. # Purpose: Describe how the script works
  676. # Arguments:
  677. #   None
  678. ##########################################
  679. usage()
  680. {
  681.         echo "Usage: $0 [ -e email ] [ -x expir_days ] [ -q ] [ -a ] [ -h ]"
  682.         echo "          {[ -d domain_namee ]} || { -f domainfile}"
  683.         echo ""
  684.         echo "  -a               : Send a warning message through email "
  685.         echo "  -d domain        : Domain to analyze (interactive mode)"
  686.         echo "  -e email address : Email address to send expiration notices"
  687.         echo "  -f domain file   : File with a list of domains"
  688.         echo "  -h               : Print this screen"
  689.         echo "  -s whois server  : Whois sever to query for information"
  690.         echo "  -q               : Don't print anything on the console"
  691.         echo "  -x days          : Domain expiration interval (eg. if domain_date < days)"
  692.         echo ""
  693. }
  694.  
  695. ### Evaluate the options passed on the command line
  696. while getopts ae:f:hd:s:qx: option
  697. do
  698.         case "${option}"
  699.         in
  700.                 a) ALARM="TRUE";;
  701.                 e) ADMIN=${OPTARG};;
  702.                 d) DOMAIN=${OPTARG};;
  703.                 f) SERVERFILE=$OPTARG;;
  704.                 s) WHOIS_SERVER=$OPTARG;;
  705.                 q) QUIET="TRUE";;
  706.                 x) WARNDAYS=$OPTARG;;
  707.                 \?) usage
  708.                     exit 1;;
  709.         esac
  710. done
  711.  
  712. ### Check to see if the whois binary exists
  713. if [ ! -f ${WHOIS} ]
  714. then
  715.         echo "ERROR: The whois binary does not exist in ${WHOIS} ."
  716.         echo "  FIX: Please modify the \$WHOIS variable in the program header."
  717.         exit 1
  718. fi
  719.  
  720. ### Check to make sure a date utility is available
  721. if [ ! -f ${DATE} ]
  722. then
  723.         echo "ERROR: The date binary does not exist in ${DATE} ."
  724.         echo "  FIX: Please modify the \$DATE variable in the program header."
  725.         exit 1
  726. fi
  727.  
  728. ### Baseline the dates so we have something to compare to
  729. MONTH=$(${DATE} "+%m")
  730. DAY=$(${DATE} "+%d")
  731. YEAR=$(${DATE} "+%Y")
  732. NOWJULIAN=$(date2julian ${MONTH#0} ${DAY#0} ${YEAR})
  733.  
  734. ### Touch the files prior to using them
  735. touch ${WHOIS_TMP}
  736.  
  737. ### If a HOST and PORT were passed on the cmdline, use those values
  738. if [ "${DOMAIN}" != "" ]
  739. then
  740.         print_heading
  741.         check_domain_status "${DOMAIN}"
  742. ### If a file and a "-a" are passed on the command line, check all
  743. ### of the domains in the file to see if they are about to expire
  744. elif [ -f "${SERVERFILE}" ]
  745. then
  746.         print_heading
  747.         while read DOMAIN
  748.         do
  749.                 check_domain_status "${DOMAIN}"
  750.  
  751.         done < ${SERVERFILE}
  752.  
  753. ### There was an error, so print a detailed usage message and exit
  754. else
  755.         usage
  756.         exit 1
  757. fi
  758.  
  759. # Add an extra newline
  760. echo
  761.  
  762. ### Remove the temporary files
  763. rm -f ${WHOIS_TMP}
  764.  
  765. ### Exit with a success indicator
  766. exit 0

Raw Paste


Login or Register to edit or fork this paste. It's free.