Linux – Sending text file contents to server using netcat

data transferlinuxnetcat

There is a daemon process listening on port 5144, which I cannot to modify.

I want to use netcat to send the contents of a text file to the server, but this causes netcat to hang the terminal until I press Ctrl+C:

cat file.txt | nc -u 127.0.0.1 5144

The only way I am able to get it to work is by running nc -u 127.0.0.1 5144 and copy/pasting the contents of the file manually.

Any ideas?


Also note:

  1. cat file.txt | ... leads to bash: ...: command not found and I can continue to use the terminal
  2. using nc -u 127.0.0.1 5144 < file.txt leads to the same behavior as using | above

Best Answer

If you are using the GNU version of netcat then you can use the -c flag to close the connection on EOF.

-c, --close close connection on EOF from stdin

If you are using the original version of the tool then you can use the -q flag.

-q secs quit after EOF on stdin and delay of secs

An example for the original version is:

cat file.txt | nc -u -q 0 127.0.0.1 5144

I have add "-q 0" to your original command. This closes the connection after the file has been sent.