How to Check SSL expiration with Bash

Written by bogkonstantin | Published 2025/04/08
Tech Story Tags: ssl | ssl-linux-certificate | tls | ssl-expiration-reminder | shell-script-for-ssl-expiry | bash-script-for-ssl-expiry | telegram-ssl-expiry-bot | ssl-certificate-expiry

TLDRThere are a few easy ways to check when an SSL certificate expires. Write Your Own Shell Script. Use a Prebuilt Script. Get Notified with a Telegram Bot.via the TL;DR App

There are a few easy ways to check when an SSL certificate expires.

Write Your Own Shell Script

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

Use a Prebuilt Script

You can also use a ready-made tool like ssl-cert-check for more features.

Get Notified with a Telegram Bot

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


Written by bogkonstantin | Web Developer | PHP Certified | Digital Nomad
Published by HackerNoon on 2025/04/08