How to pipe data over tcp from the command line?

8,127

Solution 1

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

Solution 2

Netcat should suit your needs; I don't have the documentation handy so can't be certain, but I think the sending side's command would be [...] | nc -h192.168.1.100 -p80, while the receiving side would use nc -l -p80 | [...].

Solution 3

Since you need to remote log in to at least one of the machines (to run either magicsend or magicreceive) could you just use ssh?

ssh clients are easy to come by on Windows (just install cygwin, for example.) ssh servers are also easy to come by, but may be harder to install/configure.

something like this if you are currently on the console of machine A and machine B is remote:

machine A: strarc whatever | xz -c -z - | ssh me@machineB '(xz -d -f - | strarc somethingelse)'

or if you are on the console of machine B and need to remote login to machine A then:

machine B: ssh me@machineA '(strarc whatever | xz -c -z -)' | xz -d -f - | strarc somethingelse

Share:
8,127

Related videos on Youtube

n611x007
Author by

n611x007

Updated on September 18, 2022

Comments

  • n611x007
    n611x007 almost 2 years

    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. ;) )

  • n611x007
    n611x007 about 11 years
    works quite well, however for some reason this just won't exit for me after sammy is fully read. I think it's netcat. Can it be the implementation I use causing the problem, or is this netcat's design, thus expected? Can I make it somehow exit once sammy is read (EOF), (to arbitrary file sizes and reading times)?
  • Jonathan Ben-Avraham
    Jonathan Ben-Avraham about 11 years
    @naxa: Hmm.. not clear why this should be. I don't have Windows handy but all of the documentation seems to say that also on Windows the server (B) should exit after the client (A) finishes writing to the pipe. can you post your commands? Sure you are using -l and not -L?
  • mpy
    mpy about 11 years
    I'm puzzled, why you use xz on receiver's side?! It would be much more efficient, if you compress on sender (machine A): xz -c sammy | nc 192.168.1.100 1234 and inflate on receiver (machine B): nc -l 1234 | xzcat > sammy and IMHO the intention of OP.
  • Jonathan Ben-Avraham
    Jonathan Ben-Avraham about 11 years
    @mpy: I appended your comment to my answer, thanks.
  • mpy
    mpy about 11 years
    I appreciate that, already upvoted.
  • n611x007
    n611x007 over 8 years
    but may be harder to install/configure out of curiosity did you do this? I did with cygwin and it felt kind of limited and cumbersome.