Skip to content

Instantly share code, notes, and snippets.

@apankrat
Last active September 19, 2023 06:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apankrat/062efc40f8f2dafeca8d8561b87374e7 to your computer and use it in GitHub Desktop.
Save apankrat/062efc40f8f2dafeca8d8561b87374e7 to your computer and use it in GitHub Desktop.
Bulk-check expiry dates and issuers of website SSL certificates
#! /bin/bash
#
# add your sites here, one per line
# prepend # to temporarily exclude an entry
#
sites="
reddit.com
google.com
microsoft.com
news.ycombinator.com
#twitter.com
"
tmp=/tmp/cert-check.out
now=`date -d "$now" +%s`
for site in $sites
do
if [[ $site == \#* ]]; then continue; fi
printf %-30s "$site: "
echo | openssl s_client -showcerts -servername $site -connect $site:443 2>/dev/null | openssl x509 -inform pem -noout -text > $tmp
issuer=`grep 'Issuer:' $tmp`
issuer=${issuer##*O=}
issuer=${issuer%%,*}
subject=`grep 'Subject:' $tmp`
subject=${subject##*CN=}
subject=${subject%%,*}
if [[ $site == $subject ]] || [[ ".$site" == $subject ]]; then match=' '; else match='!'; fi
expires=`grep 'Not After' $tmp`
expires=`date '+%Y-%m-%d' -d "${expires#*:}"`
epoch=`date -d "$expires" +%s`
if [ $epoch -lt $now ]
then
left='EXPIRED'
else
days=$(( ($epoch - $now) / 86400 ))
left="$days days"
fi
printf %1s $match
printf %30s "$subject | "
printf %10s "$expires | "
printf %14s "$left | "
echo " $issuer";
done
@anjanesh
Copy link

anjanesh commented Feb 24, 2023

Why does date -d "$now" +%s return

usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

?

Works on Ubuntu - date is throwing invalid format in macOS.

@apankrat
Copy link
Author

No idea, mate.

@chaeyun17
Copy link

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