As seen on XtendedGreg Youtube Live Stream: https://youtube.com/live/ldWFKRFOY-0
Netcat (nc) is a powerful networking utility available on Linux systems that allows for reading from and writing to network connections using TCP or UDP. In this guide, we'll demonstrate some of its basic functions and create a simple web server using Netcat.
- A Linux-based system with Netcat installed. You can install it using your package manager (e.g.,
apk
,apt
,yum
,dnf
).
To demonstrate basic connection establishment, open two terminal windows on the same machine:
nc -lvp 1234
This command listens ('-l') on port ('-p') 1234 for incoming connections and outputs verbose ('-v') information.
nc localhost 1234
This command connects to the listener on port 1234.
You can now type messages in Terminal 2, and they will be displayed in Terminal 1 and visa versa. It is a bidirectional communication channel based on a socket connection, so anything you send from one will appear on the other.
Netcat can be used to transfer files between systems. Let's transfer a file from one machine to another:
nc -lvp 1234 < file_to_send.txt
Replace 'file_to_send.txt' with the name of the file you want to send.
nc [sender machine IP] 1234 > received_file.txt
Replace 'receiver_ip' with the IP address of the receiving machine. This command will save the received file as 'received_file.txt'.
Netcat can also be used to create a basic HTTP server. Let's create a simple web server serving a static HTML page:
echo -e "HTTP/1.1 200 OK\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>" | nc -lvp 8080
This command will serve a basic HTML page with the content "Hello, World!" on port 8080.
while true; do echo -e "HTTP/1.1 200 OK\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>" | nc -lvp 8080; done
This command will serve a basic HTML page with the content "Hello, World!" on port 8080 and then restart the listener to accept another conneciton. It is also possible to use the -k
flag to do this without the while loop, but in both cases, it will only accept one connection at a time.
counter=0; while true; do counter=$(($counter+1)); echo -e "HTTP/1.1 200 OK\r\n\r\n<html><body><h1>Hello, World!</h1><h2>Connection Counter: $counter</body></html>" | nc -lvp 8080; done
This command will serve a basic HTML page with the content "Hello, World!" on port 8080 and then restart the listener to accept another conneciton but will increment the counter value for every connection. It is a simplistic way to add dynamic values to the page, but keep in mind that the value is updated when the next netcat listener is launched and not when the connection is made, so it may need to be combined with timeouts to force the value to update to keep the data current.
This section will cover sending and receiving multicast traffic between two machines, but switches and network equipment must be multicast enabled for this to work. In the live stream, we found out that it was not the case, so your results may vary. This is a great way to send a single message from a sender to multiple clients, but just like any UDP traffic, it may not arrive and the sender and receiver would be none the wiser.
To send multicast data using Netcat, follow these steps:
MULTICAST_ADDRESS='224.1.1.1'
PORT=12345
Replace '224.1.1.1' with your desired multicast address and 12345 with the port number you want to use.
echo "Hello, multicast world!" | nc -u -w0 -s 0.0.0.0 ${MULTICAST_ADDRESS} ${PORT}
This command sends the message "Hello, multicast world!" to the multicast address and port specified.
To receive multicast data using Netcat, follow these steps:
MULTICAST_ADDRESS='224.1.1.1'
PORT=12345
This should be the same multicast address and port used by your sender.
nc -u -l -s 0.0.0.0 -p ${PORT} ${MULTICAST_ADDRESS}
This command listens for multicast data on the specified address and port.
- Netcat is a versatile tool that can be used for various networking tasks, from basic connection establishment to file transfer and even creating simple web servers.
- Using Netcat for multicast communication allows for efficient data distribution to multiple recipients within a network environment that supports multicast.