Netcat - Basic Usage and Overview
Netcat is a versatile Unix utility that facilitates reading and writing data across network connections using either TCP or UDP protocols. Often referred to as the "Swiss Army knife" of networking, Netcat can perform a wide range of tasks, including connecting to remote servers, listening for incoming connections, and transferring files.
Netcat is a Unix utility that reads and writes data across network connections using TCP or UDP protocol.
The following tasks can be done easily with Netcat:
- Connect to a port of a target host.
- Listen to a certain port for any inbound connections.
- Send data across client and server once the connection is established.
- Transfer files across the network once the connection is established.
- Can execute programs and scripts of the client on the server and vice versa.
- Can Provide remote shell access of server to a client where shell commands can be executed.
Syntax
nc [options] [hostname] [port]
This command establishes connections or listens for incoming connections, depending on the specified options.
Basic Example
A simple client-server connection:
Type this command on the server machine.
nc -l -p 1234
Here, nc stands for Netcat, that we are calling the Netcat program. -l option tells the program to listen on a port specified by -p option. In this case, it is 1234. Now type the following on the client machine or on the other terminal:
nc 127.0.0.1 1234

This will create a TCP connection with the IP address(that is, 127.0.0.1) on the specified port(that is, 1234).
Common Options Used with Netcat
Some important options that can be used with Netcat:
1. Verbose Mode
Verbose, prints additional information about the connection.
#command for terminal 1
nc -vlp 1234
#command for terminal 2
nc -v 127.0.0.1 1234

The above command on the client is showing it has successfully connected to the server. This command can also be used to scan a port of the server if it is open or not.
2. Wait Before Terminating
After data transfer wait w seconds before terminating the connection.

#command for terminal 1
nc -w 20 -lp 1234
#command for terminal 2
nc -w 2 127.0.0.1 1234
3. Simple Chat and Data Transfer
To perform simple chat and data transfer:
#command for terminal 1
nc -lp 1234
#command for terminal 2
nc 127.0.0.1 1234

Use the above sequence of command to send the messages or data from one terminal and one ip to the other
4. File Transfer
To perform file transfer:
#command for terminal 1
nc -v -w 30 -l -p 1234 >manav.txt
#command for terminal 2
nc -v -w 2 127.0.0.1 1234<manav.txt

In this example, the server will terminate the connection 30 seconds after receiving the file. If the file is not in the current directory, then specify the entire path.
5. Execute Shell Command
To execute shell command after successful establishment of connection
#command for terminal 1
nc -lp 1234 -c /bin/sh
#command for terminal 2
nc 127.0.0.1 1234

/bin/sh is a Unix command which provides a shell to execute shell commands. This will provide a remote shell to the client, from where the client can execute shell command on the server.
Some important points on Netcat
- By default, Netcat uses TCP connection. To establish a UDP connection -u option is used.
- Without the -w option the connection doesn't terminate until quitting the Netcat program.
- -n option specifies a numerical IP address, not a domain name. That is, -n option allows only an IP address with which to connect but cannot resolve a domain name to IP address.
- -k option is used in listen mode to accept multiple connections.
Conclusion
Netcat is a powerful tool for network communication that simplifies various tasks such as connecting to ports, transferring files, and providing remote shell access. Its versatility and ease of use make it a valuable asset for network administrators and developers alike. Understanding its commands and options can significantly enhance productivity and efficiency in network management.