3

When I execute:

openssl s_client -connect google.com:443
openssl s_client -connect government.ru:443

This gives me valuable output, but I would like openssl to close the connection and exit returning an integer exit code (different in) so that I could do something like this

echo "Domain?";read d
openssl s_client -connect "$d":443

if [[ "$?" -eq 0 ]]; then
  echo "Encrypted"; do_something
else 
  echo "Plain."; do_something_else
fi

1
  • note that the failure to establish a TLS connection on port 443 doesn't mean there's an unencrypted service there or somewhere else. All it means is that TLS on 443 doesn't work. Commented Feb 16 at 16:53

2 Answers 2

3

s_client recognize the Q command. openssl docs:

Q:  End the current SSL connection and exit.
printf "Q" | openssl s_client -connect google.com:443

or:

echo | openssl s_client -connect google.com:443

or:

openssl s_client -connect google.com:443 <<< "Q"
4
  • Thanks. It end a successfully established connection (to google.com). But it hangs when a domain without a valid certificate such as government.ru is given.
    – John Smith
    Commented Feb 16 at 21:39
  • 1
    @JohnSmith Use timeout to make the command exit after n seconds.: timeout 5 openssl ...
    – GAD3R
    Commented Feb 17 at 7:46
  • 1
    I had to rewrite it: timeout 5 openssl s_client -connect "$d":443 < <(printf "Q\n") for the script to work.
    – John Smith
    Commented Feb 17 at 9:06
  • @JohnSmith see my edit
    – GAD3R
    Commented Feb 17 at 10:51
2

If you run openssl s_client -connect google.com:443 and the TLS connection is successful, the command will wait for data on standard input, for sending over the connection.

If you run openssl s_client -connect google.com:443 </dev/null and the connection is successful, it will be immediately closed, and you'll get the behaviour you're looking for.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .