Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save XtendedGreg/76e43b3451503cb9bf3ba51ce3e5da9f to your computer and use it in GitHub Desktop.
Save XtendedGreg/76e43b3451503cb9bf3ba51ce3e5da9f to your computer and use it in GitHub Desktop.
Introduction to Linux Netcat (nc) and Creating a Simple Web Server

Introduction to Linux Netcat (nc) and Creating a Simple Web Server

As seen on XtendedGreg Youtube Live Stream: https://youtube.com/live/ldWFKRFOY-0

Introduction

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.

Prerequisites

  • A Linux-based system with Netcat installed. You can install it using your package manager (e.g., apk, apt, yum, dnf).

1. Demonstrating Netcat Functions

A. Basic Connection Establishment

To demonstrate basic connection establishment, open two terminal windows on the same machine:

a) Terminal 1 (Listener):

nc -lvp 1234

This command listens ('-l') on port ('-p') 1234 for incoming connections and outputs verbose ('-v') information.

b) Terminal 2 (Client):

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.

B. File Transfer

Netcat can be used to transfer files between systems. Let's transfer a file from one machine to another:

a) On the Sender Machine:

nc -lvp 1234 < file_to_send.txt

Replace 'file_to_send.txt' with the name of the file you want to send.

b) On the Receiver Machine:

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'.

2. Creating a Simple Web Server

Netcat can also be used to create a basic HTTP server. Let's create a simple web server serving a static HTML page:

A. 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.

B. Serving a Static HTML Page to Multiple Clients

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.

C. Serving a Static HTML Page to Multiple Clients with a Dynamic Value

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.

3. Multicast Communication Using Netcat

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.

A. Sending Multicast Data

To send multicast data using Netcat, follow these steps:

a) Specify the multicast address and port:

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.

b) Send data using Netcat:

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.

B. Receiving Multicast Data

To receive multicast data using Netcat, follow these steps:

a) Specify the multicast address and port:

MULTICAST_ADDRESS='224.1.1.1'
PORT=12345

This should be the same multicast address and port used by your sender.

b) Receive data using Netcat:

nc -u -l -s 0.0.0.0 -p ${PORT} ${MULTICAST_ADDRESS}

This command listens for multicast data on the specified address and port.

Conclusion

  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment