1

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" ? – FedonKadifeli Dec 26 '19 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 '19 at 9:08
  • @karlsebal Please do not abuse the system – soldier Dec 26 '19 at 9:46
  • @karlsebal This is the public community please don't spoil if you don't even know the answer – soldier Dec 26 '19 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 '19 at 10:05
5

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"
3
  • 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 '19 at 8:12
  • updated answer... check now @soldier – Akhil Jalagam Dec 28 '19 at 9:06
  • 1
    I suggest to add rm "$certificate_file" to clean up again. – Cyrus Apr 12 at 4:01
1
#!/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"
1
  • answer seems like the same as above. it would be better to edit the accepted answer instead of adding a duplicate. @Ronen – Akhil Jalagam Apr 16 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.