Send text file, line by line, with netcat

Possa picture Possa · Dec 20, 2012 · Viewed 24.2k times · Source

I'm trying to send a file, line by line, with the following commands:

nc host port < textfile
cat textfile | nc host port

I've tried with tail and head, but with the same result: the entire file is sent as a unique line. The server is listening with a specific daemon to receive data log information.

I'd like to send and receive the lines one by one, not the whole file in a single shot.

How can I do that?

Answer

anishsane picture anishsane · Dec 20, 2012

Do you HAVE TO use netcat?

cat textfile > /dev/tcp/HOST/PORT

can also serve your purpose, at least with bash.


I'de like to send, and receive, one by one the lines, not all the file in a single shot.

Try

while read x; do echo "$x" | nc host port; done < textfile