Linux – How to pipe data over tcp from the command line

command linelinuxpipetcpwindows

I would like to pipe data from one machine in the command line to another machine over tcp. I guess I could write a socket server but this must already be implemented. For example I could use it to xz a file and send it over the network to the other side over a specified port, where I could decode and save it:

machine A: strarc -c -d:c:/windows | xz -c -z - | magicsend -p 80 -h 192.168.1.100
machine B: magicreceive -p 80 | xz -d -f - | strarc -x -d:x:/windows

I would like to do this in Linux and/or Windows with open-source tools. So Linux tools that have a[n unofficial] Windows port are preferable. 🙂

A working example command line is much appreciated.

(Note that on a Linux example I would do cat /vmlinuz instead of strarc, sure it's not quite equivalent. 😉 )

Best Answer

Use netcat. See the "CLIENT/SERVER" section of "man netcat". One machine B:

nc -l 1234 | xz -c > sammy.xz

and on machine A:

cat sammy | nc 192.168.1.100 1234

Note that there can be security implications to leaving ports open in this manner.

As mpy points out, it is more efficient in terms of network bandwidth to compress on the sending side:

xz -c sammy | nc 192.168.1.100 1234

And just save on the receiving side:

nc -l 1234 > sammy.xz
Related Question