1

I'm trying to find the expiration date of an SSL Certificate using the following command:

openssl s_client -connect ${CERT}:443 </dev/null 2>&1 | openssl x509 -text | grep "Not After"

This outputs a result to the terminal in the format:

Not After : Apr 14 10:06:09 2018 GMT

How do I remove the "Not After : " part of the result and just be shown:

Apr 14 10:06:09 2018 GMT

Any help appreciated!

tnoel999888
  • 584
  • 3
  • 12
  • 29

2 Answers2

2

Just use a lookbehind to get rid of it:

$ grep -Po '(?<=Not After : ).*' <<< "Not After : Apr 14 10:06:09 2018 GMT"
Apr 14 10:06:09 2018 GMT

With (?<=Not After : ).* we are telling grep to print everything that comes after the fixed string "Not After : ".

Note this requires a grep with the -P option to load the Perl regex.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

A simple approach:-

   openssl s_client -connect ${CERT}:443 </dev/null 2>&1 | openssl x509 -text | grep "Not After" | sed 's/Not After : //g'

Here I have added | sed 's/Not After : //g'. Now here sed command will search for string Not After : and replace it from the original string redirected to it. And final output would be:-

Apr 14 10:06:09 2018 GMT

Hope this will help you.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17