6

I'm currently trying to write a scan port result to a text file

Here is the command I tried to use:

nc -vv -z localhost 1-80 > file.txt

This doesn't work (that is, the error messages from nc don't end up in file.txt), but when I type: nc -vv -z localhost 80 > file.txt It works.

I already know that there is an output but I can't write that in a file.

  • I heard that netcat output was stderr I tried '2>' too and even '2>&1' but it's still doesn't work – Lapinou Mar 29 '15 at 16:16
  • What do you mean by "This doesn't work"? Do you mean that you cannot get the error messages from nc into file.txt? – roaima Mar 29 '15 at 16:17
  • It's doesn't write the output in a .txt file – Lapinou Mar 29 '15 at 16:17
10

You need to direct both stderr and stdout into the file:

nc -vv -z localhost 1-80 > file.txt 2>&1
  • would you mind adding the reason behind your response? – Ricardo Apr 18 '17 at 0:06
  • @Ricardo the OP wanted results (typically written to stdout) and errors (written to stderr) sent to the same file. I thought​ I'd already written this so I'm not sure what else you're looking for...? – roaima Apr 18 '17 at 8:18
  • I'm sorry for not being specific. I wanted to know why typing 80 works, compared to 1-80. – Ricardo Apr 19 '17 at 0:12
  • 2
    @Ricardo probably because port 80 didn't generate any error messages (which go to stderr), whereas scanning ports 1-80 would have. – roaima Nov 16 '17 at 20:04

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.