I want to check when week left before the certificate expires for our users on Openvpn server. On server installed Centos 6.7. I can export enddate and subject from all .crt to txt file with the command

openssl x509 -subject -enddate -noout -in filename

in format:

subject= /C=Country/ST=ca/L=City/O=company/OU=uit/CN=user/emailAddress=email
notAfter=Oct  8 13:48:27 2020 GMT

Current date on server in format - Wed May 25 10:03:06 MSK 2016 How to compare current date and date in line notAfter in txt file, and if left week or less before the certificate expires, make the txt file with name of user from the subject line with text True?

  • Valid question, perhaps; however, you're just asking for someone to do all the work for you! – ILMostro_7 May 25 '16 at 7:55

The easiest way is to convert the date string to epoch time, which is counted in seconds since 1970-01-01 00:00:00 UTC, and compare the number of seconds.

To get the current date in seconds, do:

[jenny@finch ~]$ date +%s

To get the date from the date string above, do

[jenny@finch ~]$ date -d 'Oct 8 13:48:27 2020 GMT'  +%s

A week is 604800 seconds, so check whether the difference between the two times is less than that.

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.