Send String over netcat connection

Crizly picture Crizly · May 23, 2016 · Viewed 20.4k times · Source

I have two virtual machines open, one is listening on the connection, the other connects with nc <ip> <port> from a python subprocess call. I want to send just 1 line of text over the connection then close it. I know how to send a file cat cat <file> | nc <ip> <port> but how can I just send a line of text in the nc command (without using files)?

Answer

cb0 picture cb0 · May 23, 2016

Try this:

echo -n 'Line of text' | nc <ip> <port>

You can also use temp file syntax:

cat <(echo "Line of test") | nc <ip> <port>