I am trying to send a command to netcat which will result into an output and I want to save that output to a file.

while true; do echo "showHistory userx"; done | nc -w1 xxx.xxx.xxx.xx 2222 > out.txt &

I tried a lot of google searches and read a lot of stackexchange questions but could not solve it. I played around with while, tee, mkfifo but no luck.

The file out.txt remains blank but has a lot of NUL chars

Edit: I have updated showhistory to showHistory userx in order to highlight that it is a command to be supplied over telnet/netcat session

  • You might want to use &> instead of > to send both stdout and stderr to the file. Otherwise you have to be more precise about what is not working. – pfnuesel Feb 6 '18 at 22:49
  • 1
    If I'm guessing right, your problem is that file is empty, right ? Quote from bash manual:"Background processes which attempt to read from (write to when stty tostop is in effect) the terminal are sent a SIGTTIN (SIGTTOU) signal by the kernel's terminal driver, which, unless caught, suspends the process". There's your reason - get rid of & part – Sergiy Kolodyazhnyy Feb 6 '18 at 22:50
  • 1
    And I don't know why that showhistory is needed, but yes showhistory does the same. – pfnuesel Feb 6 '18 at 22:51

I've found smth like that

for port scanning output(ex. my router)

nc -zv 1-1000 2>&1 | grep open | tee report.txt

read a report

cat report.txt 


_gateway [] 80 (http) open 
_gateway [] 22 (ssh) open

In snapshotenter image description here

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.