From 78eee7ac76708f5d778fa88ad4be4e8176e13727 Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 8 Nov 2025 21:33:33 +0000 Subject: [PATCH] =?UTF-8?q?Updated=20the=20program=20so=20it=20breaks=20ou?= =?UTF-8?q?t=20the=20config=20settings=20into=20env=20files.=20The=20scrip?= =?UTF-8?q?t=20store=20each=20TLD=20=E2=86=92=20WHOIS=20server=20mapping?= =?UTF-8?q?=20in=20an=20associative=20array,=20If=20a=20TLD=20has=20alread?= =?UTF-8?q?y=20been=20checked,=20reuse=20it=20instead=20of=20calling=20who?= =?UTF-8?q?is.iana.org=20again.=20This=20is=20to=20prevent=20such=20TLD's?= =?UTF-8?q?=20as=20.contact=20from=20not=20being=20found.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain_expiry_check.sh | 90 ++++++++++++++++++++++++++++++++++++++++++ domains.env | 3 ++ notification.env | 4 ++ 3 files changed, 97 insertions(+) create mode 100644 domain_expiry_check.sh create mode 100644 domains.env create mode 100644 notification.env diff --git a/domain_expiry_check.sh b/domain_expiry_check.sh new file mode 100644 index 0000000..cf60914 --- /dev/null +++ b/domain_expiry_check.sh @@ -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 + diff --git a/domains.env b/domains.env new file mode 100644 index 0000000..70b2156 --- /dev/null +++ b/domains.env @@ -0,0 +1,3 @@ +# domains.env +DOMAINS_FILE="/LOCATION/OF/domains.txt" +DAYS_WITHIN=30 diff --git a/notification.env b/notification.env new file mode 100644 index 0000000..40c1338 --- /dev/null +++ b/notification.env @@ -0,0 +1,4 @@ +# notification.env +SENDER_EMAIL="SENDER_NAME" +RECEIVER_EMAIL="RECEIVER_EMAIL" +WEBHOOK_URL="WEBHOOK_URL"