Skip to content

Instantly share code, notes, and snippets.

@plutocrat
Last active February 24, 2024 14:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save plutocrat/891e9bdd02bbfd4d6c3a8107501b83d0 to your computer and use it in GitHub Desktop.
Save plutocrat/891e9bdd02bbfd4d6c3a8107501b83d0 to your computer and use it in GitHub Desktop.
Bash Script to check SSL expiry dates and send a report
#!/bin/bash
# Requires openssl, bc, grep, sed, date, mutt, sort
## Edit these ##
# Space separated list of domains to check
DOMAINLIST="hp.com github.com google.com"
# Where to send the report
REPORTEMAIL=me@example.com
# Additional alert warning prepended if domain has less than this number of days before expiry
EXPIRYALERTDAYS=15
# Logfile/report location
LOGFILE=/tmp/SSLreport.txt
## Editing finished ##
# Clear last log
echo "" > $LOGFILE
for DOMAIN in $DOMAINLIST
do
EXPIRY=$( echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -noout -dates | grep notAfter | sed 's/notAfter=//')
EXPIRYSIMPLE=$( date -d "$EXPIRY" +%F )
EXPIRYSEC=$(date -d "$EXPIRY" +%s)
TODAYSEC=$(date +%s)
EXPIRYCALC=$(echo "($EXPIRYSEC-$TODAYSEC)/86400" | bc )
# Output
if [ $EXPIRYCALC -lt $EXPIRYALERTDAYS ] ;
then
echo "######ALERT####### $DOMAIN Cert needs to be renewed." >> $LOGFILE
fi
echo "$EXPIRYSIMPLE - $DOMAIN expires (in $EXPIRYCALC days)" >> $LOGFILE
done
# Report
sort -n -o $LOGFILE $LOGFILE
mutt -s "SSL Report on $(date)" $REPORTEMAIL <$LOGFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment