Last Updated: August 01, 2018
·
991
· desertlynx

Copy+paste across any networked unix computers

Need to get text from one computer to another and don't want to email it or do something equally silly? Use netcat:

Simple piping to a file

On the recipient computer, set netcat to listen on port 5000 and to pipe the contents it receives to a file.

nc -l 5000 > someTextfile.txt

On the sender computer, send the contents of the file through netcat to the other computer's ip. Note that I've used cat here, but you could just as easily use any piped data.

cat fromTextfile.txt | nc <ip of recipient computer> 5000

That's it!

Note, there's no reason to use port 5000 in particular, I'm just using it because it's free and it won't conflict with any of the lower 1-1024 ports.

Read directly into vim:

On the recipient computer, it's easy enough to extend this to pipe directly into Vim

From within Vim:

:r ! nc -l 5000

Which should read into the current buffer the given command - in this case the data being received from netcat.

Edit and pipe from vim

Taken one step further, we can pipe from one vim to another with ease so long as you have the moreutils package installed. By using vipe from this package, the contents can be piped to vim, edited and then sent onwards.

cat someFile.txt | vipe | nc <ip of recipient> 5000

Naturally, this is only one use. Binary files I suppose would work; it's a case of really only being limited by creativity; netcat is the usual one-task done well unix philosophy and there's plenty that could be expanded upon this humble idea.

6 Responses
Add your response

I had issues with 'nc -l 12345', netcat actually listened on random port. Fixed with 'nc -lp 12345'

over 1 year ago ·

I tried cinan's method.Now it worked :)

over 1 year ago ·

Nice, from what I understand, netcat is part of a number of distributions and has a number of different flavours. My setup worked with vanilla Ubuntu ~12 and ~13 and OSX but milage may vary. Which OS were you using?

over 1 year ago ·

I'm using Debian7(Wheezy),the netcat verison is [v1.10-40].Use "nc -h" you can see the options have one column "-p port local port number".

over 1 year ago ·

I like the "read-into-vim" trick, but you should note that netcat transmits whatever you're sending in cleartext, so better not use this method with sensitive data. You can add a security layer by tunnelling this through SSH, but generally you might want to consider using the scp command instead as it is secure and has bash-completion enabled for both your locally configured SSH hosts AND remote paths.

over 1 year ago ·

Good point, I was trying to transfer hashes from my laptop at the time of writing and security didn't occur to me. ssh -R is your friend for text files in this situation in my humble opinion.

over 1 year ago ·