I'm trying to send commands to a tcp port using netcat and pipe response
when I run netcat and type my command it prints response correctly but when I pass command from a pipe it sends the command correctly but doesn't print response

So, this works correctly:

netcat  localhost 9009

while this just sends command but doesn't print response:

echo 'my_command' | netcat  localhost 9009

How can I make netcat to print response text ?

  • this is probably happening to you
    – Jeff Schaller
    Commented Jun 13, 2016 at 2:10
  • @JeffSchaller: no! unfortunately using those commands doesn't help! this time it blocks forever!
    – Ariyan
    Commented Jun 13, 2016 at 2:26
  • Which netcat are you using? Unfortunately there's a dozen different variations of the netcat tool, and they don't all behave the same. Also, what is on the remote end?
    – phemmer
    Commented Jun 13, 2016 at 2:33
  • @Patrick: my netcat is OpenBSD netcat (Debian patchlevel 1.105-7ubuntu1) version; and at the remote end is telegram-cli on the same machine.
    – Ariyan
    Commented Jun 13, 2016 at 2:46
  • I think I found a man page for that netcat, but I don't see any flags which would control what I suspect is happening. I suspect that once netcat receives the EOF on STDIN, that it immediately shuts down both sides of the socket instead of doing a half-close and waiting for the remote side to close its end. If socat is an option, I would highly recommend it instead. There's only one socat, so you don't have the portability issues with there being a dozen different flavors of it, it behaves a lot more sanely, and is highly configurable.
    – phemmer
    Commented Jun 13, 2016 at 3:34

5 Answers 5


Use this:

cat <(echo command) - | nc host port

The problem is that nc will close the connection immediately after stdin is closed, which is very quick for a simple my_command string, and thus never gets the chance to receive a response. (If you pipe a very large file you will see that it might get a response before it's done sending the file).

Enter cat with - as second argument: It makes cat listen on stdin for more content to pipe through after it has sent the contents of the first argument. The first argument is just getting the echo command through cat – it could also be a file with your commands a la cat < file - | ....

Alternatively do this:

(echo command; while true; do sleep 0.01; echo -n "#"; done) | nc host port

This sends unlimited # characters on the 2nd line of the input. Using # works for a bash like remote that would ignore this as a comment. I chose a small wait time of 10 milliseconds here, so it reacts faster on connection end. YMMV.

The downside of that can be that cat or the while loop and nc keep running until you hit ^C or ^D on the shell. It really depends on the remote end.

Adding a timeout using -w 1 (OSX netcat) or -i 1 (nmap's ncat) makes it close the connection and nc after 1 second, but cat will keep running until you enter some character and the pipe breaks (I think).

However, it works if the remote side will automatically close the connection after receiving and handling the command - this will also end the nc client and the process piping into it.

This answer is based on this this answer to an identical superuser question.

  • 1
    { echo my_command; cat;} would do the same and might be considered to be easier to understand. Commented Dec 7, 2017 at 1:58

As @Patrick said, this problem is usually due to netcat exiting before the response has been given. You remedy that by adding -q 2 to the command line, i.e., tell netcat to hang around 2 seconds after detecting EOF on standard input. Obviously you can make it wait some other number of seconds as well.

  • Thanks; -q 2 worked but is it trust-able? it is making web request I can't be sure 2s is always enough! can I ?
    – Ariyan
    Commented Jun 14, 2016 at 1:13
  • 1
    Use a larger number to make it wait longer, or some negative number to make it wait indefinitely. There's also the -w option to play with. This is all in the man nc page of course. Commented Jun 14, 2016 at 6:15
  • 9
    it says invalid option -- 'q'
    – phil294
    Commented Sep 12, 2018 at 19:21
  • 1
    I think a good follow-up question is: why is nc exiting immediately instead of waiting for a response? If the connection is still open, there should be an option to make nc wait for it to close, not just for stdin to end Commented Sep 30, 2019 at 22:51
  • 1
    On Ubuntu 18.04, I used --no-shutdown and it works.
    – Ani
    Commented Jul 28, 2021 at 7:08

Different openbsd-netcat versions are quirky, needing different combinations of -w <seconds>, -q <seconds>, -N and even need different arguments depending on what is running on the other end of the connection. Using timeout options with certain versions or servers cause delays, and not using them might result in an extremely long (infinite?) delay. And I would expect different quirks with gnu netcat, but don't know if they are different between versions of it.

For example version 1.130_3 from archlinux takes extremely long (forever?) when I do this:

$ echo response | nc -l 9999 &
[1] 15190

$ time echo request | nc localhost 9999

(wait forever possibly)

But it works with -N added to either the server or the client.


I know this is a bit old but no other answer worked for me and this did:

echo 'test' | netcat -N $server $port

Notice the -N:

shutdown the network socket after EOF on the input. Some servers require this to finish their work.

Worked for me on both Windows and Linux.

Note: This is a copy paste of the answer I posted for a duplicate question.

I'm thinking this could be useful. Mods feel free to edit / delete if this is against policy or something.


I am using the ncat from nmap.

Ncat: Version 7.80 ( https://nmap.org/ncat )

The --no-shutdown option solves this issue:

echo 'test' | netcat --no-shutdown $server $port

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .