Skip to content

Commit

Permalink
Merge pull request #27 from qburst/domain-expiry-check
Browse files Browse the repository at this point in the history
Add domain expiry check script
  • Loading branch information
vyshnavlalqburst authored Oct 3, 2023
2 parents bcc895c + 7eebc09 commit f456820
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ The Opensearch snapshot script is a python script which can be used to automate
[Download logs from S3 based on timestamp](scripts/download_logs_from_s3.sh)

This script is a bash script to quickly get all logs between 2 timestamps from S3. In the many cases where you need to download logs for debugging, it will help quickly get the logs to your local.

## 11. Domain Expiry Check Script
[Domain Expiry Check Script](/scripts/domain-expiry-check/domain_expiry_checker.sh)

A bash script to monitor the expiration dates of domain names. It fetches domain information using WHOIS queries and send email alerts when domains are about to expire.
53 changes: 53 additions & 0 deletions scripts/domain-expiry-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Domain Expiry Checker

A bash script to monitor the expiration dates of domain names. It fetches domain information using WHOIS queries and send email alerts when domains are about to expire.

## Features

- Users can provide a list of domain names in the text file.
- The script will check the following conditions and sends email alerts:
- Expiring Soon: When a domain is about to expire within a specified threshold.
- Expired: When a domain has already expired.
- Expiring Today: When a domain is expiring on the same day.
- Invalid/Unable to fetch: When a given domain is invalid or it's expiry date cannot be retrieved.
- Users can customize the threshold days value.
- Send HTML formatted email alerts.
- Table like strcuture in the terminal output.

## Prerequisites
- Bash shell
- Ensure the packages ssmtp, mailutils, and mutt are installed on your system.
- Update SMTP server information in the ssmtp configuration file `/etc/ssmtp/ssmtp.conf` , refer the wiki https://wiki.archlinux.org/title/SSMTP

## Usage

1. Clone this repository.
2. Navigate to project's directory.
3. Update the domain list file path in the `domain_list` variable which should contain domains you want to monitor line by line.
4. Modify the variables `days_threshold` and `email_address` in the script `domain_expiry_checker.sh` to configure the threshold days and email address to receive alert respectively.
5. Run the script as a cronjob, you can add the following to your crontab file using `crontab -e` command to schedule the script to run daily at 4:00 AM. Replace the path of log file according to yours.
```
0 3 * * * /bin/bash /path/to/domain_expiry_checker.sh >> /path/to/logfile.log 2>&1
```

## Sample terminal output and Email alerts

- Tested on Ubuntu 20.04.6 LTS (Focal Fossa)

```
$ bash ./domain_expiry_checker.sh
+--------------------------------+------------------+--------------------+
| Domain Name | Expiry Date | Status |
+--------------------------------+------------------+--------------------+
| example.com | 2024-08-13 | OK |
| flipkart.in | 2023-09-25 | Expiring Soon |
| amazon.in | 2024-02-11 | OK |
| mcninewuwnec.com | N/A | Error |
| starbucks.au | N/A | Error |
| sancharam.in | 2023-07-25 | Expired |
| fsb333.com | 2023-06-15 | Expired |
+--------------------------------+---------------+-----------------------+
```

![Domain Expiry Alert Image](<output.png>)
84 changes: 84 additions & 0 deletions scripts/domain-expiry-check/domain_expiry_checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Path to list of domains file
domain_list="full path of domain list file"

# Set the threshold value
days_threshold=60

# Email address to receive the alert
email_address=""

# Funtion to check if they year is a Leap year or not
is_leap_year() {
year="$1"
((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
}

# Function to print a formatted table row
print_table_row() {
printf "| %-30s | %-16s | %-18s |\n" "$1" "$2" "$3"
}

# Function to check domains's expiry date and send alert
expiry_date_check() {
echo "+--------------------------------+------------------+--------------------+"
echo "| Domain Name | Expiry Date | Status |"
echo "+--------------------------------+------------------+--------------------+"

alerts=""

while read -r line
do

expiry_date=$(whois $line | grep "Registry Expiry Date" | awk -F: '{print $2}' | cut -d 'T' -f1 | xargs)
registrar_url=$(whois $line | grep -i "Registrar URL")

if [ -z "$expiry_date" ]; then
print_table_row "$line" "N/A" "Error"
alerts+="<b>Invalid/Unable to Fetch</b>: The domain <b>"$line"</b> is either invalid or we couldn't retrieve the expiry date. Please verify the domain details.<br>"

else
current_date=$(date +%s --utc)
expiry_timestamp=$(date -d "$expiry_date" +%s --utc)

days_until_expiry=$(( (expiry_timestamp - current_date) /86400 ))

year=$(date -d "$expiry_date" +%Y)

if is_leap_year "$year"; then
days_until_expiry=$((days_until_expiry + 1))
fi

if [ "$days_until_expiry" -le "$days_threshold" ] && [ "$expiry_timestamp" -ge "$current_date" ] && [ "$expiry_timestamp" != "$current_date" ]; then
print_table_row "$line" "$expiry_date" "Expiring Soon"
alerts+="<p style='color:red'><b>Expiring Soon</b>: The domain <b>$line</b> will expire on <b>$expiry_date</b> ("$days_until_expiry" days remaining). Please take action. ["$registrar_url"]<br></p>"

elif [ "$days_until_expiry" -ge "$days_threshold" ]; then
print_table_row "$line" "$expiry_date" "OK"
# No actions required

elif [ "$expiry_timestamp" -le "$current_date" ] && [ "$expiry_timestamp" != "$current_date" ]; then
print_table_row "$line" "$expiry_date" "Expired"
alerts+="<p style='color:red'><b>Expired</b>: The domain <b>$line</b> expired on <b>$expiry_date</b>. Please take action. ["$registrar_url"]<br></p>"

elif [ "$expiry_timestamp" == "$current_date" ]; then
print_table_row "$line" "$expiry_date" "Expiring today"
alerts+="<p style='color:red'><b>Expiring Today</b>: The domain <b>$line</b> is expiring <b>today</b>. Please take action. ["$registrar_url"]<br></p>"

else
# Handles any other unexpected scenario
echo "Unable to fetch "$line"'s expiry date"

fi
fi
done < "$domain_list"
echo "+--------------------------------+---------------+-----------------------+"

if [ -n "$alerts" ]; then
echo -e "$alerts" | mutt -e "set content_type=text/html" -s "Domain Expiry Alert" "$email_address"
fi

}

expiry_date_check
1 change: 1 addition & 0 deletions scripts/domain-expiry-check/domain_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<<!-- list of domains per line >>
Binary file added scripts/domain-expiry-check/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f456820

Please sign in to comment.