I have a website called xplosa.com and it has a valid SSL certificates and I went through bash script should be able to calculate remaining day counts to expire. I know there are plenty of alternative ways to do this job but I love to work with Ubuntu bash . BTW I'm using Ubuntu 18.04

This is my sample logic


get_the_cert_expiry_date() {
    # command to retrieve the expiry date

currentDate="$(date +%Y-%m-%d)"
count=$((currentDate - certExpDate))

echo "remaining days for expiry: ${count}"
  • Is this a right logic?
  • How to implement get_the_cert_expiry_date
  • 1
    Shouldn't be count="certExpDate-currentDate" ?
    – FedKad
    Dec 26, 2019 at 8:00
  • @FedonKadifeli yes can count like that but the thing is we should get the cert expiry date . the script for get date value from a website . How to do that ?
    – soldier
    Dec 26, 2019 at 9:08
  • @karlsebal Please do not abuse the system
    – soldier
    Dec 26, 2019 at 9:46
  • @karlsebal This is the public community please don't spoil if you don't even know the answer
    – soldier
    Dec 26, 2019 at 9:56
  • That's not work anymore I need a script to calculate remaining day counts for my SSL certificate expiry let's say today Dec 26th and my SSL certificate would expire in Dec 28th, then the output should be 2 days left . That's all
    – soldier
    Dec 26, 2019 at 10:05

2 Answers 2


This should work


echo -n | openssl s_client -servername "$website" -connect "$website":443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $certificate_file
date=$(openssl x509 -in $certificate_file -enddate -noout | sed "s/.*=\(.*\)/\1/")
date_s=$(date -d "${date}" +%s)
now_s=$(date -d now +%s)
date_diff=$(( (date_s - now_s) / 86400 ))
echo "$website will expire in $date_diff days"
rm "$certificate_file"
  • Output of this code tells ``` unable to load certificate 140490086364224:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: TRUSTED CERTIFICATE will expire in 0 days ```
    – soldier
    Dec 27, 2019 at 8:12
  • updated answer... check now @soldier
    – Akhil
    Dec 28, 2019 at 9:06
  • 1
    I suggest to add rm "$certificate_file" to clean up again.
    – Cyrus
    Apr 12, 2021 at 4:01
  • 1
    You could get rid of a few lines and the need to save and delete a file by having this: date=$(echo -n | openssl s_client -servername $website -connect $website:443 2>/dev/null | openssl x509 -noout -enddate | sed "s/.*=\(.*\)/\1/")
    – Joseph
    Feb 11 at 17:02

# Based on https://askubuntu.com/questions/1198619/bash-script-to-calculate-remaining-days-to-expire-ssl-certs-in-a-website

### Read Site Certificate and save as File ###
echo -n | openssl s_client -servername $1 -connect $1:443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $1.crt

### Get Full Expiratoin Date ###
date=$(openssl x509 -in $1.crt -enddate -noout | sed "s/.*=\(.*\)/\1/" | awk -F " " '{print $1,$2,$3,$4}')

### Convert Expiration Date in Epoch Format ###
date_s=$(date -j -f "%b %d %T %Y" "$date" "+%s")

### Get Curent Date in Epoch Format ###
now_s=$(date +%s)

### Calculate Time Difference ###
date_diff=$(( (date_s - now_s) / 86400 ))

echo "Certificate for $1 will expire in $date_diff days"
  • answer seems like the same as above. it would be better to edit the accepted answer instead of adding a duplicate. @Ronen
    – Akhil
    Apr 16, 2021 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.