4

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)

13

nc writes its output to standard error, you need:

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

The 2>&1 will redirect standard error to standard output so you can then pipe it to grep.

7
  • Thanks! So 2>&1 redirects stderr to stdout, cool!
    – Juicy
    Jul 31 '15 at 9:15
  • Yes. Here is something more for you to get a good understanding
    – neuron
    Jul 31 '15 at 10:53
  • 1
    @Juicy, a proper Stack Exchange "thanks!" is to mark the answer as accepted. This gives credit where credit is due.
    – Mike S
    Jul 31 '15 at 13:52
  • @MikeS at the time it had been less than 10 minutes and too early to accept an answer :)
    – Juicy
    Jul 31 '15 at 14:14
  • @Juicy, Ok sorry about that. I'll drink beer later today and chill out.
    – Mike S
    Jul 31 '15 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.