Skip to content

Instantly share code, notes, and snippets.

@theothertom
Created January 28, 2013 12:08
Show Gist options
  • Save theothertom/4655006 to your computer and use it in GitHub Desktop.
Save theothertom/4655006 to your computer and use it in GitHub Desktop.
Time in days remaining for SSL certs, for use by monitoring systems.
#!/bin/bash
host=$1
port=$2
#This is a little bit nasty with all the piping and all
cert_expire=`echo | openssl s_client -showcerts -connect $host:$port 2>/dev/null |sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -noout -subject -dates | grep notAfter | awk -F= '{print $2}'`
cert_expire_unix=`date --date="$cert_expire" +%s`
current_time_unix=`date +%s`
timediff_days=$((($cert_expire_unix - $current_time_unix) / 86400))
#For monitoring systems
echo $timediff_days
#More output when running manually
if [[ -t "0" || -p /dev/stdin ]]
then
echo "Cert for $host expires on $cert_expire" >&2
echo "Current (local) time is `date`" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment