Skip to content

Instantly share code, notes, and snippets.

@doublemarket
Created September 1, 2024 07:33
Show Gist options
  • Save doublemarket/268ece51ddf28538658f2bbb48a51ff1 to your computer and use it in GitHub Desktop.
Save doublemarket/268ece51ddf28538658f2bbb48a51ff1 to your computer and use it in GitHub Desktop.
SSL certificate expiration check script
#!/bin/bash
# ./certcheck.sh [domain name]
DOMAIN=$1
notAfter=$(echo -n Q | openssl s_client -connect ${DOMAIN}:443 -servername ${DOMAIN} 2>/dev/null | openssl x509 -noout -enddate | grep notAfter | sed 's/notAfter=//')
notAfterEpic=$(date -d "${notAfter}" +\%s)
twoWeeksLaterEpic=$(date -d 'now + 2 weeks' +\%s)
nowEpic=$(date +\%s)
echo "now = "$(date)
echo "expiry = "$(date -d @${notAfterEpic})
if [ $notAfterEpic -le $nowEpic ]; then
echo "The SSL cert for ${DOMAIN} has expired."
exit 1
elif [ $notAfterEpic -le $twoWeeksLaterEpic ]; then
echo "The SSL cert for ${DOMAIN} will expire in two weeks."
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment