The netcat has 9 lives

Netcat is an awesome utility. With nc(1) you can send and receive data over TCP or UDP using pipes (as in STDIN/STDOUT).

What does this mean to the layperson running *nix? It means quick and easy file transfers without having to set up anything up. One command for the sender, and one for the receiver. To nerds, this means all manners of wonderful scripting possibilities.

Diving in, here's how to send a single file, on the receiving computer, run:

nc -l 1234 > name-of-output-file

On the sending computer:

nc receiving.computers.hostname.com 1234 < name-of-input-file

Why do this?

Well sometimes you don't want to spend 5-15 mins setting up filesharing between two computers, especially if it's for something as simple as that.

What about directories?

Directories are a little bit different. You need to archive the directory before sending it, and then extract it before it hits the receiving computer's filesystem. Using tar(1), we can do this all, still in two commands:

tar -cf - directoryname | nc receiving.computers.hostname.com 1234

and to receive:

nc -l 1234 | tar -xf -

If you're transferring over a slow link, definitely consider compressing the data, add "z" into the tar options. Over LANs, I generally don't worry about that.

Bear in mind, firewall and IDS admins might go a bit ape if they see non-standard traffic flying around, and worse still, you might not be able to transfer files this way if in a corporate environment. Firewalls generally prevent stuff like this going on, and fair enough too. This technique could be used to steal information and that wouldn't be very nice.

What else can you do with netcat? Daemon testing, write a simple network client / server or anything else that you need to do to put data into a TCP session or UDP stream. I'll write more on that later. Until then, have fun :)