bash 在 grep 期间抑制 Openssl 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39385795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:09:02  来源:igfitidea点击:

Suppress Openssl output during grep

bashgrep

提问by ChildinTime

I often use grepfor openssl results when testing TLS security. For example:

grep在测试 TLS 安全性时,我经常使用openssl 结果。例如:

$ openssl s_client -tls1_2 -connect 172.11.15.32:443 </dev/null | grep 'IS s'
depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = root@asdasd
verify error:num=18:self signed certificate
verify return:1
depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = root@asdasd
verify return:1
DONE
Secure Renegotiation IS supported

However, the issue is, that no matter what I grep for, output always contains these (or similar) lines in the beginning:

然而,问题是,无论我用什么 grep,输出总是在开头包含这些(或类似的)行:

depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = root@asdasd
    verify error:num=18:self signed certificate
    verify return:1
    depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = root@asdasd
    verify return:1

Is it possible to somehow suppress these messages and receive only grep results as one would expect?

是否有可能以某种方式抑制这些消息并只收到预期的 grep 结果?

回答by fedorqui 'SO stop harming'

As indicated in comments, the problem is that the command openssldisplays part of its output through stderr. Then, this will show no matter what you pipe.

如评论中所示,问题在于该命令openssl通过stderr. 然后,无论您使用什么管道,这都会显示。

So if you want to just show what grephas filtered to you, you have to previously redirect stderrto /dev/nullso that it does not "jump the pipe":

因此,如果您只想显示grep已过滤的内容,则必须事先重定向stderr到,/dev/null以免它“跳过管道”:

openssl ... 2>/dev/null | grep 'IS s'
#           ^^^^^^^^^^^


See another example of this:

请参阅另一个示例:

$ touch hello
$ ls hello adlsfjaskldf
ls: cannot access adlsfjaskldf: No such file or directory  # stderr
hello                                                      # stdout

Let's grep, where everything appears:

让我们grep,一切都出现的地方:

$ ls hello adlsfjaskldf | grep hello
ls: cannot access adlsfjaskldf: No such file or directory  # stderr
hello                                                      # stdout

Let's grep but redirect stderr beforehand:

让我们先 grep 但重定向 stderr:

$ ls hello adlsfjaskldf 2>/dev/null | grep hello
hello                                      # no "ls: cannot access..." here