The netcat utility works on the same principle as the cat utility, but over the network. The simplest example of its usage is to create a server-client chat system. Netcat is a utility for port scanning and transferring information over network connections on either TCP or UDP.

 

Let us first take an advance use case of netcat, it can transfer an image of the whole hard drive over the wire using dd command.

On the sender end,

root@leo:~# dd if=/dev/sr0 | gzip -9 | nc -l -p 4039

dd -> writes cdrom image in this example

gzip -9 -> sets the highest compression level

On the receiver end,

raju@libra:~$ nc -v 192.168.40.128 4039 | pv -b > cdImage.img.gz

Connection to 192.168.40.128 4039 port [tcp/*] succeeded!

360MB

pv –b -> shows how many bytes have gone through it.

192.168.40.128 -> example IP for destination address

File transfers using nc command is not encrypted, hence it is not recommended to use on untrusted networks.

 

Now let’s move on to basis use case. To copy files over the network using netcat.

On the receiver end,

raju@libra:~$ nc -l -p 4039 > out.file

-l -> tells netcat to listen

-p -> tells netcat the port number to listen on

On the sender end,

root@leo:~# cat out.file | nc 192.168.40.129 4039 -q 10

192.168.40.129 -> example IP for destination address, can be substituted with resolvable hostname

-q -> tells netcat to quit 10 seconds after the end of the file

Leave a Reply

Your email address will not be published. Required fields are marked *