使用nc端口扫描时如何过滤成功信息

Sup*_*ish 6 grep nc

我使用以下命令对我的机器进行端口扫描

nc -zv 192.168.1.1 1-100

但我只想过滤来自以下输出的成功消息。因此我使用了以下命令

nc -zv 192.168.1.1 1-100|grep succeeded
Run Code Online (Sandbox Code Playgroud)

但没有用,它仍然显示完整的输出

nc: connect to 192.168.1.1 port 1 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 2 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 3 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 4 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 5 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 6 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 7 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 8 (tcp) failed: Connection refused
nc: connect to 192.168.1.1 port 9 (tcp) failed: Connection refused
Run Code Online (Sandbox Code Playgroud)

cof*_*Mug 8

将您的命令更改为:

nc -zv 192.168.1.1 1-100 2>&1 | grep succeeded
Run Code Online (Sandbox Code Playgroud)

2>&1原因stderr一个程序被写入到相同的文件描述符stdout。默认情况下nc写入stderr,管道只会得到,stdout因此 grep 会错过数据。

有关重定向的更多信息,请参见此处的第 3.5 节。