paint-brush
How to Check SSL expiration with Bashby@bogkonstantin
New Story

How to Check SSL expiration with Bash

by Konstantin Bogomolov2mApril 8th, 2025
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

There 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.
featured image - How to Check SSL expiration with Bash
Konstantin Bogomolov HackerNoon profile picture
0-item

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