Check_Certifiacte_Expiry/Check_Certifiacte_Expiry.sh

62 lines
2.2 KiB
Bash
Raw Normal View History

2024-07-13 08:42:20 +00:00
#!/bin/bash
# Read the list of domains from a file
domains=$(cat /www/cert_check/domains.txt)
# Email settings
sender_email="SENDER_EMAIL"
receiver_email="RECEIVER_EMAIL"
# Discord Webhook
WEBHOOK_URL="DISCORD_WEBHOOK_URL"
# 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_entry in $domains; do
# Split the domain and port
IFS=':' read -r domain port <<< "$domain_entry"
# Set default port to 443 if not specified
if [ -z "$port" ]; then
port=443
fi
# Get SSL certificate expiration date
expiration_date=$(echo | openssl s_client -servername $domain -connect $domain:$port 2>/dev/null | openssl x509 -noout -dates | grep 'notAfter' | cut -d'=' -f2)
# Calculate the number of days left until the SSL certificate 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 SSL certificate 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:$port: $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="SSL Certificate Expiration Alert - $current_datetime"
body="The following domains have SSL certificates 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"
# Send the notification to Discord
curl -H "Content-Type: application/json" -X POST --data "{\"content\": null, \"embeds\": [{\"title\": \"$subject\", \"description\": \"$body\", \"color\": null}], \"attachments\": []}" $WEBHOOK_URL
fi