2

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

#!/bin/bash

get_the_cert_expiry_date() {
    # command to retrieve the expiry date
}

currentDate="$(date +%Y-%m-%d)"
website="xplosa.com"
certExpDate="$(get_the_cert_expiry_date)"
count=$((currentDate - certExpDate))

echo "remaining days for expiry: ${count}"
  • Is this a right logic?
  • How to implement get_the_cert_expiry_date
5
  • 1
    Shouldn't be count="certExpDate-currentDate" ?
    – FedKad
    Commented 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
    Commented Dec 26, 2019 at 9:08
  • @karlsebal Please do not abuse the system
    – soldier
    Commented 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
    Commented 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
    Commented Dec 26, 2019 at 10:05

2 Answers 2

8

This should work

#!/bin/bash

website="xplosa.com"
certificate_file=$(mktemp)
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"
4
  • 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
    Commented Dec 27, 2019 at 8:12
  • updated answer... check now @soldier
    – Akhil
    Commented Dec 28, 2019 at 9:06
  • 1
    I suggest to add rm "$certificate_file" to clean up again.
    – Cyrus
    Commented 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
    Commented Feb 11, 2022 at 17:02
2
#!/bin/bash

# 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"
4
  • answer seems like the same as above. it would be better to edit the accepted answer instead of adding a duplicate. @Ronen
    – Akhil
    Commented Apr 16, 2021 at 4:53
  • 1
    There is no -j option in date command.
    – FedKad
    Commented Mar 24, 2023 at 12:23
  • date -j seems to exist in BSD/MacOS: stackoverflow.com/a/43927574/5155484 Commented Jun 22, 2023 at 9:13
  • The working version for other systems is: date_s=$(date -d "%b %d %T %Y" -d "$date" "+%s") Commented Jun 22, 2023 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .