Netcat flexibility
I thought I’d take a moment to write a quick blurb about a wonderfully flexible and useful utility I use somewhat frequently: netcat. Netcat is a relatively easy to use network utility. It allows quick creation of network connections. It’s most useful to me when used in conjunction with other programs to quickly tack on network capabilities. Have you ever had to copy a directory tree quickly from one machine to another on a LAN? Most people would choose scp or rsync over ssh for something like this. What about when the tree size is really quite large and you would like to squeeze every ounce of speed out of the copy? If encryption is not important (e.g. on a LAN), why waste the CPU cycles on it? One trick to avoid encryption for these copy tasks is to use netcat with tar. For example, say I had a somewhat large directory structure on my work box and I wanted to copy the raw bytes across the network to my laptop using netcat. On the laptop, after changing directories to where I want the tree extracted, I would fire up netcat to listen on an arbitrary port and to pipe any incoming data to tar for extraction. The command to do this might look like:
nc -l -p 3333 | tar -xvf -
On my work computer, if in my current directory I had a sub-directory named ‘tree’, I would execute the following command:
tar -cvf - tree | nc <laptop_ip> 3333
This command tells tar to create an archive of the directory ‘tree’ and output the resultant archive to stdout (-). We then pipe that output to netcat which sends it across the wire to <laptop_ip> on port 3333. Netcat on the laptop receives the archive byte stream and passes that byte stream to the running instance of tar (tar -xvf -) which will take the byte stream and extract that archive to the current directory. Done deal. I use this quite often for LAN copies and I feel I must repeat that this is probably not a good idea if you are copying sensitive data across the internet. Use rsync over ssh or plain old scp for those instances. I’m sure there are many more cases where you will find netcat to be the right utility for the job. I find that many utilities I use on a Linux system are very good at a singular purpose. I find it extremely elegant that Linux (and other *NIX variant) systems were designed to allow this kind of modularity and easy cooperation between thousands of programs by a simple concept such as piping. It makes me happy.
Until later.

