The netcat linux command

One of the neatest tools that I’ve found is the program netcat. It is a way to pipe data over the network simply and easily. You don’t need a fancy protocol like CIFS, just stream the data over the LAN.

Netcat is a “full duplex” connection, meaning it both sends and receives using the connection. It “cross-connects” the standard in of one computer to the standand out of the other and vice versa.

You specify one computer as the listener, and one as the receiver.

Type “echo hi there | nc -v -l -p 5000” on one computer, then “echo receiving you loud and clear | nc -v 192.168.1.1 5000” on the other.

On the “listening side”:

nc -v means verbose (tells you what ports are being listened on)
-l means listen
-p specifies the port to listen on
5000 specifies the port number

nc -v means verbose (tells you what ports are being connected to)
192.168.1.1 is the IP address to connect to
5000 is the IP port number to connect.

If you want to use the same computer, open up two lxterminal windows and pretend they are different computers, using “localhost” or “127.0.0.1” as your IP address.

Type “echo hi there | nc -v -l -p 5000” in one terminal window, then “echo receiving you loud and clear | nc -v 127.0.0.1 5000” on the other.

It really doesn’t matter who is the “listener” because as I mentioned, netcat “cross connects” the standard input of one side to the standard output of the other side.

There are examples abound on the internet, but here’s one that I particularly like: imaging hard drives over the network.

So on one computer (the one to image) I type: (note that the -q 0 is necessary to close the link when stdin is closed, otherwise the
connection will stay open)

“dd if=/dev/hda | nc -v -l -p 5000 -q 0”

and on the destination computer I type:

“nc 192.168.1.1 5000 > /media/sda1/MY_IMAGE_FILE”

And if you want to reverse it, where the sending computer is the “client” if you will,

type this on the destination computer first (because it has to be listening before you connect to it)
“nc -v -l -p 5000 > /media/sda1/MY_IMAGE_FILE”

and

“dd if=/dev/hda | nc 192.168.1.2 5000 -q 0” on the sending computer.

If you have a knoppix with the pv program (pipe viewer) you can throw a pv into the pipeline to check the progress.

“dd if=/dev/hda | pv -b | nc -v -l -p 5000” for example.

And to test if your image is correct, use your good friend md5sum.

md5sum /dev/hda

md5sum /media/sda1/MY_IMAGE_FILE

Remember that you need to be very careful with the dd program…. they don’t call it disk destroyer for nothing… Practice your dd skills on a test system. I’ve clobbered my hard drive before by typing something wrong and there isn’t any undo… good thing I had a backup.

I found this useful to play with the tomsrtbt floppy linux distribution and image an old laptop over the network using tomsrtbt on the old laptop and a 3c589 pcmcia card and knoppix on the other computer. Tomsrtbt doesn’t have the CIFS network protocol, so netcat comes in handy. Tomsrtbt has a slightly different format for the nc command (netcat is also called nc). You don’t need to specify the -p option.

This should get you started, but go check out the netcat page on wikipedia. You’ll find lots of good examples. There are other netcat type programs out there, such as ncat (which is also present on knoppix 7.2 dvd).

If your netcat gets “stuck” (like you forgot the -q 0 parameter) and you want to close it, a simple ctrl-c will make it close the connection.


4 Count Case GE ProLine 2 Bulb 120V T12 Electronic Fluorescent Light Ballasts picture

4 Count Case GE ProLine 2 Bulb 120V T12 Electronic Fluorescent Light Ballasts

$49.99



Sylvania Quicktronic Pro Series Ballast QHE 2X59T8/UNV ISN-SC picture

Sylvania Quicktronic Pro Series Ballast QHE 2X59T8/UNV ISN-SC

$10.00



Triad B232IUNVHP-N 2-Lamp Outdoor Electronic Ballast T8 / ES 120-277V 60Hz picture

Triad B232IUNVHP-N 2-Lamp Outdoor Electronic Ballast T8 / ES 120-277V 60Hz

$20.00



AccuStart B254P347-D 1-2 Lamp Program Rapid Start T5/HO Ballast 347V -NIB of 10 picture

AccuStart B254P347-D 1-2 Lamp Program Rapid Start T5/HO Ballast 347V -NIB of 10

$200.00



QHE 4X32T8/UNV ISH-SC Sylvania 51347 4-Lamp T8 Instant Start Fluorescent Ballast picture

QHE 4X32T8/UNV ISH-SC Sylvania 51347 4-Lamp T8 Instant Start Fluorescent Ballast

$10.79



Fulham Workhorse 5 Solid State Ballast WH5-120-L picture

Fulham Workhorse 5 Solid State Ballast WH5-120-L

$20.99



Philips ADVANCE AmbiStar RELB-2S40-N Replacement Ballast 40-Watt 2-Lamp T12 picture

Philips ADVANCE AmbiStar RELB-2S40-N Replacement Ballast 40-Watt 2-Lamp T12

$18.79



10 PACK- 49455 Sylvania QHE 4X32T8/UNV PSH-HT 4-Lamp Programmed Start T8 Ballast picture

10 PACK- 49455 Sylvania QHE 4X32T8/UNV PSH-HT 4-Lamp Programmed Start T8 Ballast

$24.48



10-Count GE Fluorescent Ballast, GE432-MVPS-L Electronic T8, 120v to 277v picture

10-Count GE Fluorescent Ballast, GE432-MVPS-L Electronic T8, 120v to 277v

$108.99



Inter-Global IG13-20EL electronic ballast 120v New Common Aquarium Light Ballast picture

Inter-Global IG13-20EL electronic ballast 120v New Common Aquarium Light Ballast

$19.95



Powered by WordPress. Designed by WooThemes