Search code examples
bashwhoisdomainexpiration

Bash script, domain expiration date with email sending


I am trying to implement a solution for automatic mail sending once it finds a domain that expiration date has been exceeded. I am really new to this, therefore I managed to get as far as code below, that shows expiration dates and sends an email containg the output.

The kind of help I am looking for is at least a clue how to compare expiration date with the current date and get a result as number of days. I will really appreciate any kind of help.

#!/bin/bash
DOM="onet.pl wp.pl"
for d in $DOM
do
  echo -n "$d - "
  whois $d | egrep -i 'Expiration|Expires on' | head -1
   whois $d | egrep -i 'Expiration|Expires on' | head -1 >> /tmp/domain.date
  echo ""
done
#[ -f /tmp/domain.date ] && mail -s 'Domain renew / expiration date' [email protected] < /tmp/domain.date || :

Solution

  • Look no further than the date command, it has everything you need !

    Here is a straightforward solution using date -d to parse the date :

    # Get the expiration date
    expdate="$(whois $d | egrep -i 'Expiration|Expires on' | head -1)"
    # Turn it into seconds (easier to compute with)
    expdate="$(date -d"$expdate" +%s)"
    # Get the current date in seconds
    curdate="$(date +%s)"
    # Print the difference in days
    printf "Number of days to expiration : %s\n" "$(((expdate-curdate)/86400))"
    

    Good luck !