I'm using nc
to scan for open ports but I'm scanning a wide range and it's displaying too many results. Trying to grep
it for the word "succeeded" doesn't work for some reason:
$ 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
...
(I thought about sending the error messages to /dev/null
as well: nc -zv localhost 31000-32000 2>/dev/null
. But in that case there are no results whatsoever. It seems that all nc
port status messages are error/debug messages)
Best Answer
nc
writes its output to standard error, you need:The
2>&1
will redirect standard error to standard output so you can then pipe it togrep
.