我使用以下命令对我的机器进行端口扫描
nc -zv 192.168.1.1 1-100
但我只想从以下输出中过滤成功的消息。因此我使用了以下命令
nc -zv 192.168.1.1 1-100|grep succeeded
但没有用,仍然显示完整输出
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
答案1
将命令更改为:
nc -zv 192.168.1.1 1-100 2>&1 | grep succeeded
2>&1
stderr
程序被写入与 相同的文件描述符的原因stdout
。默认情况下nc
写入stderr
,管道只会获取,stdout
因此 grep 将丢失数据。
有关更多信息,请参阅此处第 3.5 节关于重定向的所有信息。