Updated the program so it breaks out the config settings into env files. The script store each TLD → WHOIS server mapping in an associative array, If a TLD has already been checked, reuse it instead of calling whois.iana.org again. This is to prevent such TLD's as .contact from not being found.

This commit is contained in:
Phil 2025-11-08 21:33:33 +00:00
parent 4cfaf71b06
commit 78eee7ac76
3 changed files with 97 additions and 0 deletions

90
domain_expiry_check.sh Normal file
View File

@ -0,0 +1,90 @@
#!/bin/bash
# Load environment variables
source /PATH/TO/domains.env
source /PATH/TO/notification.env
# Read the list of domains
domains=$(cat "$DOMAINS_FILE")
# Initialize
declare -A whois_cache # TLD → WHOIS server cache
expiring_domains=()
current_datetime=$(date +%Y-%m-%d_%H:%M:%S)
# --- Functions ---
# Get the authoritative WHOIS server for a TLD (with in-memory cache)
get_whois_server() {
local domain=$1
local tld="${domain##*.}"
# Return cached entry if present
if [[ -n "${whois_cache[$tld]}" ]]; then
echo "${whois_cache[$tld]}"
return
fi
# Query whois.iana.org for authoritative WHOIS server
local server
server=$(whois -h whois.iana.org "$tld" 2>/dev/null | grep -i "whois:" | awk '{print $2}' | head -n1)
# Store result in cache (even if blank)
whois_cache[$tld]="$server"
echo "$server"
}
# Get the expiration date for a domain from the correct WHOIS server
get_expiration_date() {
local domain=$1
local whois_server=$2
local whois_output
if [[ -n "$whois_server" ]]; then
whois_output=$(whois -h "$whois_server" "$domain" 2>/dev/null)
else
whois_output=$(whois "$domain" 2>/dev/null)
fi
echo "$whois_output" | grep -Ei 'Expiration Date|Expiry Date|Registry Expiry Date' | awk -F ':' '{print $2}' | head -n1
}
# --- Main Loop ---
for domain in $domains; do
whois_server=$(get_whois_server "$domain")
expiration_date=$(get_expiration_date "$domain" "$whois_server")
# Calculate days left
if [[ -n "$expiration_date" ]]; then
expiration_timestamp=$(date -d "$expiration_date" +%s 2>/dev/null)
if [[ -n "$expiration_timestamp" ]]; then
days_left=$(( (expiration_timestamp - $(date +%s)) / 86400 ))
else
days_left=""
fi
fi
# Check if expiring soon
if [[ -n "$days_left" && "$days_left" -lt "$DAYS_WITHIN" ]]; then
expiring_domains+=("$domain: $days_left days left (WHOIS: ${whois_server:-default})")
fi
done
# --- Notifications ---
if [[ ${#expiring_domains[@]} -gt 0 ]]; then
subject="Domain Expiration Alert - $current_datetime"
body="The following domains are expiring within $DAYS_WITHIN days:\n\n${expiring_domains[@]/%/\\n}"
# Email
echo -e "$body" | mail -s "$subject" -a "From: $SENDER_EMAIL" "$RECEIVER_EMAIL"
# Discord
curl -H "Content-Type: application/json" \
-X POST \
--data "{\"content\": null, \"embeds\": [{\"title\": \"$subject\", \"description\": \"$body\"}]}" \
"$WEBHOOK_URL"
fi

3
domains.env Normal file
View File

@ -0,0 +1,3 @@
# domains.env
DOMAINS_FILE="/LOCATION/OF/domains.txt"
DAYS_WITHIN=30

4
notification.env Normal file
View File

@ -0,0 +1,4 @@
# notification.env
SENDER_EMAIL="SENDER_NAME"
RECEIVER_EMAIL="RECEIVER_EMAIL"
WEBHOOK_URL="WEBHOOK_URL"