6

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

1 Answer 1

8

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.

3
  • what is purpose of 2>&1 in this command Commented Jun 29, 2016 at 12:16
  • @P.G.Krish See updated answer. Commented Jun 29, 2016 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 Commented Jun 29, 2016 at 12:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.