How to monitor domain expiry date using shell script in Linux

The shell is an interface that helps users to operate the system through different commands, scripts, and utilities provided by Linux and Unix-based operating systems.

For most of the Linux distros, Bash shell is the popular and default shell. You can identify your current shell using the following command.

$ echo $SHELL

A domain is the identification string that is used to uniquely address your websites within the internet. When you buy the domain, the domain service provider will provide a domain for a limited period of time and need to renew it once it expires. Many online tools can be found to check the validity of the domain but also you can check it using the terminal. In this article, we will create a bash shell script that gives info about the validity of the domain.

We will use the whois command-line tool to check the domain expiry date. The whois command will return the detailed information on the domain we then need to parse the date from it using the grep command to view the expiration date.

$ whois google.com | egrep -i "Expiration | Expire on"

Get Domain expiration date

Now, let's ready the shell script that shows us the expiry date of the domain. First, create and open the file domaincheck.sh in your preferred editor. The editor will create the new file for you if it does not exist on write and quit the file.

$ vim domaincheck.sh

The shell script always starts with #!bin/bash. Copy and paste the following script in your script.

#!/bin/bash

#Specify all the domains you want to check
DOMAINS="google.com dribbble.com facebook.com youtube.com"

current_epoch=`date '+%s'`
for dm in $DOMAINS
do
  expiry_date=`whois $dm | egrep -i "Expiration Date:|Expires on"| head -1 | awk '{print $NF}'`
  echo -n " $dm - Expires on $expiry_date "
  expiry_epoch=`date --date="$expiry_date" '+%s'`
  epoch_diff=`expr $expiry_epoch - $current_epoch`
  days=`expr $epoch_diff / 86400`
  echo " $days days remaining. "
done

In the above script, I have declared the variable that contains a list of domains and also the variable that contains the current time value in the epoch. The date command is formatted to Unix epoch time using the +%s formatting option and wraps the command with ` ` so that output of the command won’t be displayed on the terminal.

Next, I have used a for loop to get each domain name from DOMAINS. Inside the loop I have used the whois command to fetch info on the domain piping the output using egrep with -i to filter the specific text, head with -1 to print only the first line of the output, and awk command to get the last field. With this, you will be able to fetch the date from the whois command output which is assigned to the variable name expiry_date. The awk is the popular scripting language used for formatting the data and creating the reports. Then, I have printed the date using the echo command to view the corresponding domain expiry date.

In the next line, I have parsed the string to date using the date command with --date option and format it into Unix epoch time. The expr command will do the basic operations like addition, subtraction, division, etc, and subtracting expiry_epoch value with current_epoch value will result in the remaining time. Then convert the epoch time into day by dividing it by 86400. Lastly, print the result using the echo command.

Once you execute the script you will get to see the following result.

$ bash domaincheck.sh

Domain check result

Conclusion

The Unix epoch time is the number of seconds from 00:00:00 UTC on 1 January 1970 to till now. Different command-line tools can be found to check the domain info on the domain but in this article, the whois command is used to display domain info. You can now also modify and improve the script according to your need if you have good knowledge of scripting.

Leave a Comment