Netcat is a generic utility capable to doing anything involving TCP/UDP connections. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning.
Syntax:
netcat [flags] [hostname] [port]
or
nc [flags] [hostname] [port]
Flags:
-v ----> Include a verbose o/p to understand what netcat is doing
-z ----> Check if the specified port [port] is open on server [hostname]
What can 'netcat' do?
It can be used a tool for any of the following
- Port Scanning.
- File Transfer
- Chat
When '-z' flag is specified, netcat reports whether the specified ports are open or not.
Example:
hprem@hprem-lnx:~$ netcat -v -z hprem-lds 20-30
netcat: connect to hprem-lds port 20 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 21 (tcp) failed: Connection refused
Connection to hprem-lds 22 port [tcp/ssh] succeeded!
netcat: connect to hprem-lds port 23 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 24 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 25 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 26 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 27 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 28 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 29 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 30 (tcp) failed: Connection refused
2)Chat
To start a chat session between two hosts. For that start netcat in listen mode on some random port On host1 .In the following examples it is assumed that the machine that creates the session(listening connection) has the IP address 192.168.1.4 . So, create the chat server on this machine and set it to listen to 9600 TCP port::
netcat -l 9600
On the other host, start netcat On the same port:
netcat 192.168.1.4 9600
Anything that is typed on host2 would get echoed on host1 and vice-versa
3)File Transfer
To transfer file between two hosts, start netcat in listen mode on the host that should accept the file, on some random available port and redirect the o/p to a file
On host1:
netcat -l 9600 > filename
On the other server, start netcat and as it to read data from the file that has to be copied
On host2:
netcat 192.168.1.4 9600 < filename
netcat does not show any info about the progress of the data transfer. This is inconvenient when dealing with large files. In such cases, a pipe-monitoring utility like pv can be used to show a progress indicator.