Skip to content

Instantly share code, notes, and snippets.

@xman1980
Created January 15, 2024 15:04
Show Gist options
  • Save xman1980/baa2493a418cab9f20830ccbb3af9de1 to your computer and use it in GitHub Desktop.
Save xman1980/baa2493a418cab9f20830ccbb3af9de1 to your computer and use it in GitHub Desktop.
domain SSL cert checker | bash script
#!/bin/bash
DOMAINS="nodes.txt"
RECIPIENT="[email protected]"
DAYS="14"
# Mailgun settings
MAILGUN_API_KEY=""
MAILGUN_DOMAIN=""
MAILGUN_URL="https://api.mailgun.net/v3/$MAILGUN_DOMAIN/messages"
# Slack Webhook URL
SLACK_WEBHOOK_URL=""
while read -r TARGET; do
echo "checking if $TARGET expires in less than $DAYS days"
expirationdate=$(date -d "$(: | openssl s_client -connect "$TARGET":443 -servername "$TARGET" 2>/dev/null \
| openssl x509 -text \
| grep 'Not After' \
| awk '{print $4,$5,$7}')" '+%Y-%m-%d')
in14days=$(date -d "+$DAYS days" '+%Y-%m-%d')
echo "Expiration date: $expirationdate"
echo "In 14 days: $in14days"
if [[ "$in14days" > "$expirationdate" ]]; then
subject="Certificate expiration warning for $TARGET"
body="KO - Certificate for $TARGET expires in less than $DAYS days, on $expirationdate"
# Send email using Mailgun
curl -s --user "api:$MAILGUN_API_KEY" \
"$MAILGUN_URL" \
-F from="[ApiVPN] SSL Checker Bot <mailgun@${MAILGUN_DOMAIN}>" \
-F to=$RECIPIENT \
-F subject="$subject" \
-F text="$body"
# Send message to Slack
slack_message="Certificate for $TARGET expires in less than $DAYS days, on $expirationdate"
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"${slack_message}\"}" $SLACK_WEBHOOK_URL
else
echo "OK - Certificate for $TARGET expires on $expirationdate"
fi
done < "${DOMAINS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment