grep 在 nc 输出中不匹配

grep 在 nc 输出中不匹配

我用来nc扫描开放端口,但扫描范围很广,并且显示了太多结果。由于某种原因,尝试grep“成功”一词不起作用:

$ nc -zv localhost 31000-32000 | grep succeeded
...
nc: connect to localhost port 31957 (tcp) failed: Connection refused
nc: connect to localhost port 31958 (tcp) failed: Connection refused
nc: connect to localhost port 31959 (tcp) failed: Connection refused
Connection to localhost 31960 port [tcp/*] succeeded!
nc: connect to localhost port 31961 (tcp) failed: Connection refused
nc: connect to localhost port 31962 (tcp) failed: Connection refused
nc: connect to localhost port 31963 (tcp) failed: Connection refused
nc: connect to localhost port 31964 (tcp) failed: Connection refused
...

/dev/null(我也考虑过将错误消息发送到: nc -zv localhost 31000-32000 2>/dev/null。但在这种情况下,没有任何结果。似乎所有nc端口状态消息都是错误/调试消息)

答案1

nc将其输出写入标准错误,您需要:

nc -zvv localhost 31000-32000 2>&1 | grep succeeded

会将2>&1标准错误重定向到标准输出,以便您可以将其通过管道传输到grep.

相关内容