Created
July 17, 2018 02:56
-
-
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.
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 | |
# 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 |
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
Chuck this in your cron to check every morning at 8am and send you a slack if anything is amiss:
See my slack-me gist for that script.