How to check SSL/TLS certificate expiry using openssl

Use the following command to check the expiry date of a SSL/TLS certificate. This command also includes SNI (server name indication)

echo | openssl s_client -connect 2>&1 | sed --quiet '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -noout -text | grep --after 2 Validity

Remember to replace both instances of by the domain name to test!

Example output:

    Not Before: Aug 18 00:00:00 2021 GMT
    Not After : Aug 17 23:59:59 2022 GMT