2016-09-08 47 views
1

在测试TLS安全性时,我经常使用grep获得openssl结果。例如:在grep期间抑制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 = [email protected] 
verify error:num=18:self signed certificate 
verify return:1 
depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = [email protected] 
verify return:1 
DONE 
Secure Renegotiation IS supported 

然而,问题是,不管我用grep的,输出始终包含在开始的时候,这些(或类似)行:

depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = [email protected] 
    verify error:num=18:self signed certificate 
    verify return:1 
    depth=0 C = US, ST = asd, O = Billing, CN = asdasd, emailAddress = [email protected] 
    verify return:1 

是否有可能以某种方式抑制这些消息并只接收grep结果,如人们所期望的那样?

+1

这可能是因为它的大部分输出到stderr,它经过不'grep'使得它的任何动作(它只是检查标准输出)。只需使用'openssl 2>/dev/null'重定向它,然后相应地grep。 – fedorqui

+0

@fedorqui这很快,并且确实解决了问题,谢谢! – ChildinTime

回答

3

如注释中所示,问题在于命令openssl通过stderr显示其部分输出。然后,这将显示,不管你管什么。

所以,如果你只想展示一下grep已过滤的话,你必须事先重定向stderr/dev/null所以它不会“跳管”:

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

见另一个例子这个:

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

让我们的grep,这里的一切似乎:

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

让我们的grep,但stderr重定向事先:

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