KenD
KenD

Reputation: 5318

How to terminate OpenSSL s_client after connection

(Reviewers: I also know this is straying into SuperUser territory, but if the previous question snuck through ... :) )

This is very similar to this question, but in an Windows (7/8/Server 2008/2012) environment: I'm using the Windows port of OpenSSL.

I'm running

openssl s_client -connect 192.168.0.1:443

from a command prompt, in order to show certificate information. However, openssl waits for user input afterwards; I can Ctrl+C to "break" the output, or every just type a few characters and hit return, but I need to automate this - all I'm really interested in is the certificate information.

As per the previous question, I need some way to terminate/close the connection. However, I've tried piping in input files, echoing/typeing input into the mix, and nothing seems to simulate a real user. Can anyone show me how to force openssl to exit after connecting?

Upvotes: 66

Views: 54019

Answers (3)

MortenB
MortenB

Reputation: 3529

On windows I use this snippet to get the output into a variable, it writes Q as the first line, but it terminates:

$v = powershell.exe -command '&{write-host "Q" | C:\\Program` Files\\OpenSSL-Win64\\bin\\openssl.exe s_client --connect mysite:443}'
$v[0]
Q
$v[-1]   
read R BLOCK
$v.length
114

Upvotes: 0

Degenerate DevOps
Degenerate DevOps

Reputation: 1312

You can achieve the desired effect by using a pipe to pass in the character "Q". This makes for a great one-liner for a script:

echo "Q" | openssl s_client -connect host:port

If you are using a sufficiently new version of BASH, you can also use the triple less-than redirect instead of piping (some times a pipe isn't usable since it operates on stdin/stdout):

openssl s_client -connect host:port <<< "Q"

Upvotes: 118

JSAnderson
JSAnderson

Reputation: 338

Entering the letter 'Q' at the beginning of a blank line will end an active connection. I've seen s_client get into states where this does not do anything, but this is the documented way to quit a session.

If you want to do this in batch mode, just create a text file with the letter 'Q' followed by a carriage return and direct it into the end of the command like so:

openssl s_client -connect host:port < Q.txt

I tried this and it works.

Upvotes: 7

Related Questions