Monday, September 9, 2013

Simple copy of file between linux boxes with netcat

I am still pretty new all this Linux sysadmin stuff, so when I get new tricks to to help me I clap my hands and quickly write it down. So here goes.

If you need to copy large amounts between instances, you might not have access to copy between the specified instances via scp. Here netcat, nc, can help you. It opens a direct connection between the two instances, so you can stream bit from one to the other.


I want to send file1.txt from instance A to instance B.

On instance B do:

$ tar -cf - . | nc A_ip 4000

On instance A do:

$ nc -l 4000 | tar xvf file1.txt

If you need to send over a lot of files you can use the following on instance A:

$ nc -l 4000 | tar xvf -

If you are sending a a huge amount of data, you might need to throw gzip and gunzip in to the mix:

$ tar -cf - . | gunzip | nc A_ip 4000
$ nc -l 4000 | gzip |tar xvf file1.txt

Of course you need to make sure the ports are open for communication, but most instances have at least one open port that can be used. Just make sure the port is not use by other programs.

Note that netcat syntax differs a bit between the original unix version and the OpenBSD version. The above examples are in the OpenBSD variant.



No comments: