Skip to content

Instantly share code, notes, and snippets.

@jrwarwick
Last active December 9, 2020 19:35
Show Gist options
  • Save jrwarwick/3c2e4198da112474abc0e10dccb3e84c to your computer and use it in GitHub Desktop.
Save jrwarwick/3c2e4198da112474abc0e10dccb3e84c to your computer and use it in GitHub Desktop.
SSL Cert Catalog with Expiry Checking
#!/usr/bin/bash
function monthnumber {
month=$(echo ${1:0:3} | tr '[a-z]' '[A-Z]')
MONTHS="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
tmp=${MONTHS%%$month*}
month=${#tmp}
monthnumber=$((month/3+1))
printf "%02d\n" $monthnumber
}
# Or at the expense of some flexibility and brevity, you get more readability:
function monthnumber2 {
case $(echo ${1:0:3} | tr '[a-z]' '[A-Z]') in
JAN) monthnumber="01" ;;
FEB) monthnumber="02" ;;
MAR) monthnumber="03" ;;
APR) monthnumber="04" ;;
MAY) monthnumber="05" ;;
JUN) monthnumber="06" ;;
JUL) monthnumber="07" ;;
AUG) monthnumber="08" ;;
SEP) monthnumber="09" ;;
OCT) monthnumber="10" ;;
NOV) monthnumber="11" ;;
DEC) monthnumber="12" ;;
esac
printf "%02d\n" $monthnumber
}
TODAY=$( date "+%Y%m%d" )
echo "GET /" | openssl s_client -connect github.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > tmp.pem
cert_expiry_date=$(openssl x509 -in tmp.pem -noout -enddate | cut -d'=' -f2)
month_name=$(echo $cert_expiry_date | cut -d' ' -f1)
month_number=$( monthnumber $month_name )
cert_expiration_datestamp=$( echo $cert_expiry_date | awk "{printf \"%d%02d%02d\",\$4,\"${month_number}\",\$2}" )
echo "compare: [ $cert_expiration_datestamp -gt $TODAY ]"
if [ $cert_expiration_datestamp -gt $TODAY ] ; then
echo "all ok, the cert expiration date is in the future."
else
echo "WARNING: cert expiration date is in the past."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment