domain_checker/check.sh

49 lines
1.6 KiB
Bash
Raw Normal View History

2023-04-22 21:16:21 +00:00
#!/bin/bash
# Read the list of domains from a file
domains=$(cat /LOCATION/OF/domains.txt)
# Email settings
sender_email="SENDER_NAME"
receiver_email="RECEIVER_EMAIL"
# Initialize an empty array to store the expiring domains and their days left
expiring_domains=()
# Set number of days to search within
days_within=30
# Set time and date of search
current_datetime=$(date +%Y-%m-%d_%H:%M:%S)
# Iterate over the list of domains
for domain in $domains; do
# WHOIS query to get the expiration date
expiration_date=$(whois $domain | grep -Ei 'Expiration Date|Expiry Date|Registry Expiry Date' | awk -F ':' '{print $2}')
# Calculate the number of days left until the domain expires
if [ ! -z "$expiration_date" ]; then
expiration_timestamp=$(date -d "$expiration_date" +%s 2>/dev/null)
if [ ! -z "$expiration_timestamp" ]; then
days_left=$((($expiration_timestamp - $(date +%s)) / 86400))
fi
fi
# Check if the domain is expiring within set days time
if [ ! -z "$days_left" ] && [ "$days_left" -lt $days_within ]; then
# Add the expiring domain and its days left to the array
expiring_domains+=("$domain: $days_left days left")
fi
done
# Send an email with the expiring domains and their days left (if any)
if [ ${#expiring_domains[@]} -gt 0 ]; then
# Email content
subject="Domain Expiration Alert - $current_datetime"
body="The following domains are expiring within $days_within days:\n\n${expiring_domains[@]/%/\\n}"
# Send the email
echo -e "$body" | mail -s "$subject" -a "From: $sender_email" "$receiver_email"
fi