Skip to content

Instantly share code, notes, and snippets.

@askin
Last active October 3, 2019 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save askin/d5efb7f37b61520a4fc87dd0dcd132f9 to your computer and use it in GitHub Desktop.
Save askin/d5efb7f37b61520a4fc87dd0dcd132f9 to your computer and use it in GitHub Desktop.
Check SSL certificate expiry date for a domain
#!/bin/bash
# Simple SSL cert days-till-expiry check script
# by Askin Ozgur https://blog.yollu.com askin@askin.ws
# Derived from Glen Scott, www.glenscott.net
PORT=443
if [ ${#} == 2 ]; then
DOMAIN=$1
PORT=$2
elif [ ${#} == 1 ]; then
DOMAIN=$1
else
echo "Usage: $0 example.tld [port]"
exit 1
fi
# Check port is valid?
if ! [[ ${PORT} == +([0-9]) ]]; then
echo "Port must be numeric!!!"
exit 1
fi
openssl_output=$(echo "
GET / HTTP/1.0
EOT" \
| openssl s_client -connect ${DOMAIN}:${PORT} -servername ${DOMAIN} 2>&1);
if [[ "$openssl_output" = *"-----BEGIN CERTIFICATE-----"* ]]; then
cert_expiry_date=$(echo "$openssl_output" \
| sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' \
| openssl x509 -enddate \
| awk -F= ' /notAfter/ { printf("%s\n",$NF); } ');
seconds_until_expiry=$(echo "$(date --date="$cert_expiry_date" +%s) - $(date +%s)" |bc);
days_until_expiry=$(echo "$seconds_until_expiry/(60*60*24)" |bc);
echo "$days_until_expiry";
else
echo "NOT_FOUND";
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment