#!/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