
There are a few easy ways to check when an SSL certificate expires.
Here's a simple script that checks the expiration date of an SSL certificate:
#!/bin/sh
TARGET=$1
if [ -z "$TARGET" ]; then
echo "Provide a host as a parameter"
exit 1
fi
echo "Checking $TARGET..."
expirationdate=$(openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -text \
| grep 'Not After' \
| awk '{print $4,$5,$7}')
if [ -z "$expirationdate" ]; then
echo "Failed to retrieve certificate expiration date for $TARGET"
exit 1
fi
echo "Certificate expires on $expirationdate"
Usage:
Save the script as ssl.sh
Make it executable:
sudo chmod +x ssl.sh
Run it:
./ssl.sh example.com
You can also use a ready-made tool like ssl-cert-check for more features.
Want to automate monitoring and get notified before a certificate expires? Use Telegram Bot that can monitor and notify you about TLS/SSL certificate expiration