Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'd like to automate the following process with the openssl command:

I've been working along the lines of the following command (here wrapped in python):

local("echo -n | openssl s_client -connect google.com:443 -certform DER | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/google.com.der")

but it doesn't give me what I expect.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Your sed command is only capturing openssl s_client's standard output, but the certificate comes with the other debugging information on standard error. Have you tried something like this?

openssl s_client -connect google.com:443 </dev/null 2>&1 |
  sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' |
  openssl x509 -outform DER >/tmp/google.com.der
share|improve this answer
    
That worked. Thank you! –  Danny Armstrong Jan 11 '13 at 6:39

You're probably seeing the lines which openssl outputs on stderr and are ignored by sed. You can just get rid of these before passing the output to sed:

% echo | openssl s_client -connect google.com:443 -certform DER 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
-----BEGIN CERTIFICATE-----
MIIF/DCCBWWgAwIBAgIKUCYyawAAAAB1rzANBgkqhkiG9w0BAQUFADBGMQswCQYD
...
v6UdT7iXlxVh2oslpIksSw2bf0H7PUQQjFCMLVbiEMAQbXal4+SsyYB+GsPVvlR+
-----END CERTIFICATE-----
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.