notes on javascript, linux, and more

4.20.2008

Netcat, the network swiss army knife

What is netcat?


From the official GNU page:


Netcat is a featured networking utility which reads and writes data across network connections, using the TCP/IP protocol.


Have you ever been in a bind, where you need to transfer a file between two networked computers, but neither of them has any file transfer services running? You could install and configure SMB, NFS, or FTP, but if you just need to transfer a few files occasionally, or you are working with very limited system resources, perhaps it is best to just use netcat.


However, netcat is not just a file transfer utility. It’s beauty lies in it’s simplicity; you can use netcat to pipe data across a network, in the exact same way that you can pipe data on a Unix or Windows shell. This makes it extremely simple for novice scripters to transfer any kind of data over the network, even continuous streams of data.


Here’s how to send a file to hostname or IP address ‘foo’, for some port number ‘bar’.

On ‘foo’, run this command:


nc -lp bar > /path/to/save/file/to


Then on the file sender:


cat /path/to/original/file | nc foo bar


So, what else can you do with netcat? Here’s a fun example: on some machine with a microphone connected, and mixer levels set accordingly:


cat /dev/dsp | netcat -d -h foo -p bar


And then on some other machine, ‘foo’, with the firewall configured to allow TCP connections to port ‘bar’:


netcat -lp bar > /dev/dsp


What does this do?

It streams your microphone input to the speakers of ‘foo’. At least, it should in theory. I don’t have a way to test it right now, but let me know if it works for you. It may require slight modification. Careful that the speakers and microphone aren’t in the same room, or you’ll get feedback!


Here’s another fun example: a very tiny webserver (modified example from http://www.stearns.org/doc/nc-intro.current.html).


printf "Content-Type: text/html\n\n" > ~/mywebpage.html

echo 'hello world' >> ~/mywebpage.html

while true ; do cat ~/mywebpage.html | nc -l -p 80 | head –bytes 2000 >> /tmp/requests ; date>> /tmp/requests ; done


Of course, I can’t test that right now either…


Well, hope you had fun, and I hope it worked. Let me know if you found any modifications that were necessary to get things to work right. Cheers.

No comments:

About Me

My photo
chicago, il, United States
I'm a software engineer by profession.

Labels