diff --git a/scripts/devops-tools/ssl_expiry.sh b/scripts/devops-tools/ssl_expiry.sh index b8c298e..b7f1fcf 100644 --- a/scripts/devops-tools/ssl_expiry.sh +++ b/scripts/devops-tools/ssl_expiry.sh @@ -5,19 +5,61 @@ # url: https://www.ssllabs.com/ssltest/ echo "=== SSL Expiry Checker ===" + read -p "Enter domain (e.g. google.com): " domain + if [ -z "$domain" ]; then domain="google.com" fi + echo "Connecting to $domain:443..." echo "" + +# Check if openssl is installed if ! command -v openssl >/dev/null 2>&1; then echo "Error: openssl utility is not installed." exit 1 fi -res=$(echo | openssl s_client -servername "$domain" -connect "$domain":443 2>/dev/null | openssl x509 -noout -dates -issuer) -if [ -z "$res" ]; then + +# Fetch certificate details +cert_info=$(echo | openssl s_client -servername "$domain" -connect "$domain:443" 2>/dev/null | openssl x509 -noout -dates -issuer) + +# Verify certificate was retrieved +if [ -z "$cert_info" ]; then echo "Failed to retrieve SSL certificate details." -else - echo "$res" + exit 1 +fi + +# Display certificate information +echo "$cert_info" +echo "" + +# Extract expiry date +expiry_date=$(echo "$cert_info" | grep "notAfter" | cut -d= -f2) + +# Convert expiry date to Unix timestamp +expiry_ts=$(date -d "$expiry_date" +%s 2>/dev/null) +current_ts=$(date +%s) + +# Validate timestamp conversion +if [ -z "$expiry_ts" ]; then + echo "Unable to calculate certificate expiry." + exit 1 fi + +# Calculate remaining days +days_left=$(( (expiry_ts - current_ts) / 86400 )) + +echo "Days Remaining: $days_left" +echo "" + +# Display certificate health status +if [ "$days_left" -lt 0 ]; then + echo "❌ EXPIRED: Certificate has already expired!" +elif [ "$days_left" -le 7 ]; then + echo "🚨 CRITICAL: Certificate expires within 7 days!" +elif [ "$days_left" -le 30 ]; then + echo "⚠ WARNING: Certificate expires within 30 days!" +else + echo "✅ HEALTHY: Certificate is valid." +fi \ No newline at end of file