Command line script way to get SSL certificate

Last Modified: Wed, 22 Aug 2018 12:56:32 +0000 ; Created: Thu, 16 Mar 2017 16:21:01 +0000

openssl s_client -showcerts -connect 2>/dev/null </dev/null| sed -ne '/BEGIN CERT/,/END CERT/p'

This will obtain all certificates in the chain that the remote server is sending. Typically the root CA is not (nor should be) included.

The 2>/dev/null removes output from the openssl command so you can get only the ASCII BASE64 encoded certificate.

The </dev/null disables STDIN input so the openssl command terminates after a successful connection.