0

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' myemail@gmail.com < /tmp/domain.date || :
ashaneen
  • 137
  • 1
  • 15

1 Answers1

2

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 !

Marc Coiffier
  • 194
  • 1
  • 5
  • Thank You very much for help, still get a result i have no idea what is actually wrong, could You take a look please? onet.pl - option expiration date: 2019.08.16 16:52:03 date: invalid date ‘option expiration date: 2019.08.16 16:52:03’ Days for expiration : -17641 – ashaneen Apr 20 '18 at 07:42