With openssl:
openssl x509 -enddate -noout -in file.pem
The output is on the form:
notAfter=Nov 3 22:23:50 2014 GMT
Also see MikeW's answer for how to easily check whether the certificate has expired or not, or whether it will within a certain time period, without having to parse the date above.
Answer from that other guy on Stack OverflowWith openssl:
openssl x509 -enddate -noout -in file.pem
The output is on the form:
notAfter=Nov 3 22:23:50 2014 GMT
Also see MikeW's answer for how to easily check whether the certificate has expired or not, or whether it will within a certain time period, without having to parse the date above.
If you just want to know whether the certificate has expired (or will do so within the next N seconds), the -checkend <seconds> option to openssl x509 will tell you:
if openssl x509 -checkend 86400 -noout -in file.pem
then
echo "Certificate is good for another day!"
else
echo "Certificate has expired or will do so within 24 hours!"
echo "(or is invalid/not found)"
fi
This saves having to do date/time comparisons yourself.
openssl will return an exit code of 0 (zero) if the certificate has not expired and will not do so for the next 86400 seconds, in the example above. If the certificate will have expired or has already done so - or some other error like an invalid/nonexistent file - the return code is 1.
(Of course, it assumes the time/date is set correctly)
Be aware that older versions of openssl have a bug which means if the time specified in checkend is too large, 0 will always be returned (https://github.com/openssl/openssl/issues/6180).
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"
#!/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"
It's just few lines of code, and it works like a charm. This is what I am planning to do:
-
add error and exception handling (Yes in bash command line)
-
maybe add a gui using dialog but not sure if this is possible will see.
-
What else?
I don't want to use rust etc as I don't know them and I don't have free time to invest on it. All I am planning is to create some bash projects that I can list in my resume. I am 1.5 yoe support production implementor