-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from qburst/domain-expiry-check
Add domain expiry check script
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<<!-- list of domains per line >> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.