Skip to content

Instantly share code, notes, and snippets.

@caseyfw
Created July 17, 2018 02:56
Show Gist options
  • Save caseyfw/0f8af522842b52bd8359a893b27540bf to your computer and use it in GitHub Desktop.
Save caseyfw/0f8af522842b52bd8359a893b27540bf to your computer and use it in GitHub Desktop.
SSL certificate expiry checker - warns when a cert is expiring in less than 7 days.
#!/bin/bash
# Number of days from expiry to throw warning.
warnPeriod=7
domainsFile="$( cd "$(dirname "$0")" ; pwd -P )/domains.txt"
if [ ! -f "$domainsFile" ]; then
>&2 echo "Missing domains.txt file."
exit 1
fi
exitCode=0
while read domain; do
expiry=$(echo | openssl s_client -connect $domain:443 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
expiryDate=$(echo $expiry | date +"%-d %b %Y" -f - )
expiryTimestamp=$(echo $expiry | date +%s -f -)
nowTimestamp=$(date +%s -u)
diffTimestamp=$(expr $expiryTimestamp - $nowTimestamp)
diffDays=$(echo "$diffTimestamp / 86400" | bc)
echo "$domain: $expiryDate ($diffDays days)"
if [ $diffDays -le $warnPeriod -a $diffDays -gt 0 ]; then
>&2 echo "WARNING: $domain expires in $diffDays days!"
((exitCode++))
fi
done < $domainsFile
exit $exitCode
@caseyfw
Copy link
Author

caseyfw commented Jul 17, 2018

Chuck this in your cron to check every morning at 8am and send you a slack if anything is amiss:

0 8 * * * /home/<username>/bin/ssl-expiry-check.sh 2>&1 >/dev/null | /home/<username>/bin/slack-me

See my slack-me gist for that script.

@caseyfw
Copy link
Author

caseyfw commented Jul 17, 2018

Script makes output like:

caseyfulton.com: 12 Sep 2018 (57 days)
www.caseyfulton.com: 12 Sep 2018 (57 days)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment