-
-
Save wojtek-fliposports/21c87202c132bef370ff745936f83c4a to your computer and use it in GitHub Desktop.
Bash SSL Certificate Expiration Check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
help_text(){ | |
echo "Usage:" | |
echo " $0 HOSTNAME:PORT" | |
echo " HOSTNAME - target hostname to check" | |
echo " ECHO - target port. Default 443" | |
echo "" | |
echo "Env variables:" | |
echo " CERT_CHECK_SLACK_URL - Slack Webhook" | |
echo " CERT_CHECK_SLACK_USERNAME - Slack username" | |
echo " CERT_CHECK_SLACK_CHANNEL - Slack channel" | |
} | |
TARGET="$1" | |
if [[ "${TARGET}" == "" ]]; then | |
help_text | |
exit 1 | |
fi | |
HOSTNAME=${TARGET%%:*} | |
PORT=${TARGET##*:} | |
if [[ "${HOSTNAME}" == "${PORT}" ]] ; then | |
PORT=443 . # ':' not found | |
fi | |
SLACK_URL=${CERT_CHECK_SLACK_URL:-'Your incoming webhook'} # 'https://hooks.slack.com/services/SOMETHING/SOMETHING ELSE' | |
SLACK_USERNAME=${CERT_CHECK_SLACK_USERNAME:-'CERT CHECK'} | |
SLACK_CHANNEL=${CERT_CHECK_SLACK_CHANNEL:'#alerts'} | |
emoji=":terminator:" | |
DAYS=7; | |
expirationdate=$(date -d "$(: | openssl s_client -connect $HOSTNAME:$PORT -servername $HOSTNAME 2>/dev/null \ | |
| openssl x509 -text \ | |
| grep 'Not After' \ | |
| awk '{print $4,$5,$7}')" '+%s'); | |
in7days=$(($(date +%s) + (86400*$DAYS))); | |
if [ $in7days -gt $expirationdate ]; then | |
message="Certificate for $HOSTNAME:$PORT expires in less than $DAYS days" | |
payload="payload={\"channel\": \"$SLACK_CHANNEL\", \"username\": \"$SLACK_USERNAME\", \"icon_emoji\": \"$emoji\", \"text\": \"${message}\"}" | |
curl -m 5 --data-urlencode "${payload}" $SLACK_URL | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment