Netcat (nc) for file transfer and other dummy examples

Netcat ( ) is used in order to write and read over the network...


Computer B read (captures) :
  nc -l -p <PORT>     (reads from tcp <PORT> and write to <stdout>)
Computer A write (sends):
  nc <IP> <PORT>    (writes to <IP>:<PORT> what reads from <stdin>)

Since nc can read and write on standard input/output you can use pipe in order to stream data over the network.

nc chain : stdin-> network-> stdout
nc chain : stdin-> network-> stdout

 Notes :

  • netcat is the nc command
  • if you want to chain together two computers you have to run first the READ command on B and after WRITE command on A line

#1 Example : Hello World!

print "Hello world!" (over the net)

Computer B read (captures) :
-l -p <PORT>
Computer A write (sends):
 "Hello world!" | nc <IP> <PORT>

Step by Step

1) computer B ( nc -l -p 8181
computer B remains in listening for  something to read from tcp <PORT>

Computer B
Computer B

2) computer A: echo 'hello world!' | nc 8181
computer A writes something to (

Computer A
Computer A

3) when execute command on A computer B has something to read ... so it shows "hello word!" on stdout

Computer B
Computer B

#2 Example : transfer File

Computer B (captures files) :
 nc -l -p 8080 | tar -x
Computer A (source of files) :
 tar -c . | nc 8080
(please check the correct  options for your tar)

Sure you can use compression (z option) but you have to keep in mind that this could be a wrong choice. If you are transfering files to a slow computer (for example you telneted  a NAS with a slow ARM) the compression could  consume too CPU... and copy result very slow. Moreover the bottleneck is not always, and in any case the network (the compression allow to transfer less bytes over the network), ... somethime bottleneck is disk or CPU.

#3 Example : cat

Computer B (recieves cat) :
 nc -l -p 8181 | more
Computer A (source of cat) :
 cat <filename> | nc 8181

Powered by WP Review