diff --git a/scripts/README.md b/scripts/README.md index 55ba592..516c835 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -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. \ No newline at end of file diff --git a/scripts/domain-expiry-check/README.md b/scripts/domain-expiry-check/README.md new file mode 100644 index 0000000..f9e75df --- /dev/null +++ b/scripts/domain-expiry-check/README.md @@ -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]() diff --git a/scripts/domain-expiry-check/domain_expiry_checker.sh b/scripts/domain-expiry-check/domain_expiry_checker.sh new file mode 100755 index 0000000..614f2d4 --- /dev/null +++ b/scripts/domain-expiry-check/domain_expiry_checker.sh @@ -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+="Invalid/Unable to Fetch: The domain "$line" is either invalid or we couldn't retrieve the expiry date. Please verify the domain details.
" + + 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+="

Expiring Soon: The domain $line will expire on $expiry_date ("$days_until_expiry" days remaining). Please take action. ["$registrar_url"]

" + + 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+="

Expired: The domain $line expired on $expiry_date. Please take action. ["$registrar_url"]

" + + elif [ "$expiry_timestamp" == "$current_date" ]; then + print_table_row "$line" "$expiry_date" "Expiring today" + alerts+="

Expiring Today: The domain $line is expiring today. Please take action. ["$registrar_url"]

" + + 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 diff --git a/scripts/domain-expiry-check/domain_list.txt b/scripts/domain-expiry-check/domain_list.txt new file mode 100644 index 0000000..033e6d5 --- /dev/null +++ b/scripts/domain-expiry-check/domain_list.txt @@ -0,0 +1 @@ +<