diff --git a/Check_Certifiacte_Expiry.sh b/Check_Certifiacte_Expiry.sh new file mode 100644 index 0000000..59d064d --- /dev/null +++ b/Check_Certifiacte_Expiry.sh @@ -0,0 +1,61 @@ +#!/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 diff --git a/domains.txt b/domains.txt new file mode 100644 index 0000000..6329781 --- /dev/null +++ b/domains.txt @@ -0,0 +1,4 @@ +example.com +subdomain.example.com:8443 +imap.example.com:993 +smtp.example.com:465