Wednesday 7 March 2012

How to open a port in linux machine

You can use netcat (nc) to open a port in linux machines.

# nc -k -l 1000  -------- port number 1000 is open in local machine

it can manipulate, create or read/ write TCP/IP connections.

 File transfer

Copy a large folder over a raw tcp connection. The transfer is very quick (no protocol overhead) and you don't need to mess up with NFS or SMB or FTP or so, simply make the file available on the server, and get it from the client. Here 192.168.0.5 is the server IP address.

server#  tar -cf - -C VIDEO_TS . | nc -l -p 4444  
# Serve tar folder on port 4444
client#  nc 192.168.0.5 4444 | tar xpf - -C VIDEO_TS 
# Pull the file on port 4444

server#  cat largefile | nc -l 5678                # Server a single file
client#  nc 192.168.0.5 5678 > largefile      # Pull the single file

server#  dd if=/dev/da0 | nc -l 4444   # Server partition image
client#  nc 192.168.0.5 4444 |dd of=/dev/da0
# Pull partition to clone
client#  nc 192.168.0.5 4444 |dd of=da0.img   # Pull partition to file

Chat feature using nc

google and yahoo can chat over a simple TCP socket. The text is transferred with the enter key.

google# nc -lp 4444
yahoo # nc 192.168.1.1 4444





1 comment: