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
Suppress Openssl output during grep
提问by ChildinTime
I often use grep
for 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 openssl
displays 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 grep
has filtered to you, you have to previously redirect stderr
to /dev/null
so 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