4

I used following command for port scanning of my machine

nc -zv 192.168.1.1 1-100

but I want to filter only succeeded message from following output.Hence i used the following command

nc -zv 192.168.1.1 1-100|grep succeeded

But no use, still it shows full output

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
7

Change your command to this:

nc -zv 192.168.1.1 1-100 2>&1 | grep succeeded

2>&1 causes stderr of a program to be written to the same file descriptor as stdout. nc writes to stderr by default, pipe will only get stdout hence grep will miss the data.

See section 3.5 here for more info All about redirection.

  • what is purpose of 2>&1 in this command – SuperKrish Jun 29 '16 at 12:16
  • @P.G.Krish See updated answer. – coffeMug Jun 29 '16 at 12:21
  • But the terminal shows error messages as well as succeeded messages, why we do another redirection for mixing error with output messages – SuperKrish Jun 29 '16 at 12:31

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.