Reading and writing are the basic operations that can be applied to data in every system. TCP is abbreviated as Transmission Control Protocol and is a connection-oriented communication protocol that makes it easier for computers on a network to send and receive messages. It is the most widely used protocol in networks that employ the Internet Protocol (IP); it is sometimes referred to as TCP/IP when used jointly. UDP, abbreviated as User Datagram Protocol, is a communication protocol used to construct low-latency and loss-tolerant connections between applications on the internet.
Here at Ibmi Media, we shall look into "Netcat", command which is used to read and write disks across networks using TCP and UDP protocols on Ubuntu 20.04 (Linux OS).
More about Netcat ?
Netcat can be a useful tool for any IT team, though the growth of internally managed network services and cloud computing make that particular environment a natural fit.
The Netcat utility application includes several instructions for managing networks and monitoring the level of data between systems. The TCP and UDP protocols are the basis of computer networks, like the internet. It is regarded as the Swiss army knife of networking tools and is among the most effective tools in the armory of network and system administrators. Netcat is a cross-platform program that runs on Linux, Windows, Mac OS X, and BSD. Netcat can be used to debug and analyze connectivity issues as well as scan for open ports, transfer data, and act as a proxy.
How to use Netcat to Read and Write Data Across the Network ?
On macOS and common Linux distributions such as Ubuntu and Debian, the Netcat package comes pre-installed. Some of the "Netcat" utilities are mentioned below:
- Perform Port Scanning through Netcat.
- Sending Files through Netcat.
- Create a web server through Netcat.
It's Syntax is:
$ nc [options] host port
Either "nc" or "netcat" is used on Ubuntu systems.
A TCP connection will be established to provide host/hosts and port/ports by Netcat as default.
Use the -u option if you want to create a UDP connection:
$ nc -u host port
1. Perform Port Scanning through Netcat
One of the most prevalent Netcat applications is port scanning. You have the option of scanning a single port or a range of ports.
a. For TCP, to Scan for open ports:
To scan the open ports in the range of 30-60 using Netcat, run the command mentioned below:
$ nc -z -v 10.0.2.15 30-60
- -z will instruct NC to just scan for open ports and not to send any data to them.
- -v tells the info about verbose.
Filter the result using the grep command:
$ nc -z -v 10.0.2.15 2>&1 | grep succeeded
b. For UDP:
Simply add the -u parameter to the script to check for UDP ports in the below-mentioned command:
$ nc -z -v -u 10.0.2.15 30-60
2. Send Files through Netcat
By establishing a basic client or server model, Netcat may be used to transport data from one host to another. This is accomplished using the -l option on the receiving host to set Netcat to listen on a certain port, then creating a standard TCP connection from multiple computers and transferring the file across it.
Run the below-mentioned command on the receiving end, which opens port 6666 for incoming connections and diverts the output result to the file:
$ nc -l 6666 > linux1.txt
Linux1.txt is the filename to be opened for writing, and you can change the filename according to your requirements.
Now receiving host will be connected to sending host and sends the file:
$ nc google.com 6666 < linux2.txt
3. Create Web Server Through Netcat
Firstly, create a simple HTML "linux, html" file by using the nano command:
$ nano linux.html
Type the below mentioned content or you can add content according to your requirement following the html file rules:
Save by "Ctrl+S" and close the file by "Ctrl+X":
$ printf 'HTTP/1.1 200 OK\n\n%s' "$(cat linux.html)" | netcat -l 8888
Now in the browser, you can access the file by below-mentioned link: