diff options
-rwxr-xr-x | domainexpiry.sh | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/domainexpiry.sh b/domainexpiry.sh new file mode 100755 index 0000000..d5fa29c --- /dev/null +++ b/domainexpiry.sh @@ -0,0 +1,118 @@ +#!/bin/bash +set -euo pipefail + +# ==== Variables ==== +# Output warning if fewer than this number of seconds until expiry +WARNSECONDS=1209600 # Two weeks +# File containing a newline separated list of host:port combinations to be checked +DOMAINS="domains.txt" +# ==== Variables ==== + +# === Not variables === +SUPPORTEDTLDS=(.co.uk .com .eu .info .je .net .org .ovh .uk) +# === Not variables === + + +#for TLD in "${SUPPORTEDTLDS[@]}" ; do +# echo "DEBUG: Supported TLD: $TLD." +#done + +# Loop through all host:port combinations +while read -r DOMAIN ; do + + echo "DEBUG: DOMAIN: $DOMAIN." + + # Work out the TLD + TLD="" + DOTS=0 + for TLDTEST in "${SUPPORTEDTLDS[@]}" ; do +# echo "DEBUG: Checking $DOMAIN against $TLDTEST." + if echo "$DOMAIN" | grep -q "$TLDTEST$" ; then +# echo "DEBUG: $DOMAIN may be a $TLDTEST domain..." + DOTSTEST=$(echo "$TLDTEST" | grep -o "\." | wc -l) +# echo "DEBUG: $TLDTEST has $DOTSTEST dots compared to the previous $DOTS." + if [ "$DOTSTEST" -gt "$DOTS" ] ; then +# echo "DEBUG: Yes, $TLDTEST is the current winner." + TLD="$TLDTEST" + DOTS="$DOTSTEST" + fi + fi + done + + if [ "$TLD" == "" ] ; then + echo "ERROR: Unknown TLD for $DOMAIN, ignoring." + continue + else + echo "DEBUG: $DOMAIN is a $TLD domain." + fi + + # Get the WHOIS data + WHOIS="$(whois $DOMAIN)" + + # Do the appropriate WHOIS grepping magic based on what the TLD was + case "$TLD" in + .co.uk) +# echo "DEBUG: Doing the .co.uk domain stuff" + DATESTR=$(echo "$WHOIS" | grep -E '^\s*Expiry date:' | sed -r 's/^\s*Expiry date:\s*//') +# echo "DEBUG: DATESTR: $DATESTR" + EXPIRY=$(date --date "$DATESTR" +%s) +# echo "DEBUG: $DOMAIN expires on $EXPIRY aka "$(date --date @"$EXPIRY")"." + ;; + .je) +# echo "DEBUG: Doing the .je domain stuff" + # .je WHOIS returns two dates, so pick the earlier one just in case + # Strip out st/nd/rd/th (e.g. 30th -> 30) for parsing with `date` + DATESTR=$(echo "$WHOIS" | grep -E '^\s*Registry fee due on' | sed -r 's/\s*Registry fee due on //; s/ each year//; s/(st |nd |rd |th )/ /') + echo "DEBUG: DATESTR: $DATESTR" + # Same again, plus the original registration year (e.g. 30th January 2011 -> 30 January) + DATESTR2=$(echo "$WHOIS" | grep -E "^\s*Registered on" | sed -r 's/\s*Registered on //; s/ [0-9]+//; s/(st |nd |rd |th )/ /') + echo "DEBUG: DATESTR2: $DATESTR2" + # These date only strings will be assumed to be this year by `date` + EXPIRY=$(date --date "$DATESTR" +%s) + EXPIRY2=$(date --date "$DATESTR2" +%s) + # Pick whichever is lower (i.e. sooner) + if [ "$EXPIRY" -gt "$EXPIRY2" ] ; then + EXPIRY="$EXPIRY2" + # Keep the original chosen DATESTR for use later + DATESTR="$DATESTR2" + fi + # If this date is in the past, assume it actually expires next year + NOW=$(date +%s) + if [ "$EXPIRY" -lt "$NOW" ] ; then + YEAR=$(date +%Y) + YEAR=$((YEAR+1)) + EXPIRY=$(date --date "$DATESTR $YEAR" +%s) +# echo "DEBUG: Chosen DATESTR: $DATESTR $YEAR" +# else +# echo "DEBUG: Chosen DATESTR: $DATESTR" + fi +# echo "DEBUG: $DOMAIN expires on $EXPIRY aka "$(date --date @"$EXPIRY")"." + ;; + .uk) + echo "DEBUG: Doing the .uk domain stuff" + DATESTR=$(echo "$WHOIS" | grep -E '^\s*Expiry date:' | sed -r 's/^\s*Expiry date:\s*//') +# echo "DEBUG: DATESTR: $DATESTR" + EXPIRY=$(date --date "$DATESTR" +%s) +# echo "DEBUG: $DOMAIN expires on $EXPIRY aka "$(date --date @"$EXPIRY")"." + ;; + *) + echo "ERROR: Unknown TLD for $DOMAIN, I have no idea how we got here, exiting." + exit + ;; + esac + + NOW=$(date +%s) +# echo "DEBUG: NOW: $NOW." + + # Number of seconds left + DIFFERENCE=$(("$EXPIRY" - "$NOW")) +# echo "DEBUG: DIFFERENCE: $DIFFERENCE." + + # Warn if less than WARNSECONDS less + if [ "$DIFFERENCE" -lt "$WARNSECONDS" ] ; then + echo "Warning! The domain $DOMAIN expires in $DIFFERENCE seconds (~$((DIFFERENCE / 60 / 60 / 24)) days)." + else + echo "DEBUG: The domain $DOMAIN expires in $DIFFERENCE seconds (~$((DIFFERENCE / 60 / 60 / 24)) days)." + fi + +done < "$DOMAINS" |