Netcat is a versatile tool - I learned about it way too late in my career. It can be used as a TCP-server or client. As HTTP is based on TCP, Netcat can even be used to have some fun with HTTP:
This is how to tell nc
to listen to new connections on port 8080
:
$ nc -l 8080
Pointing your Browser or another HTTP client like curl on localhost:8080
:
$ curl localhost:8080/index.html
Netcat will show you the incoming HTTP request immediately:
$ nc -l 8080 GET /index.html HTTP/1.1 Host: localhost:8080 User-Agent: curl/7.64.1 Accept: */*
Lets create an actual file index.html
and serve it via Netcat:
# Create the file $ cat << EOF > index.html <!doctype html> <html> <body> <h1>Hello from Netcat!</h1> </body> </html> EOF
# Serve index.html $ while true; do cat index.html | nc -l 8080 done
Directing the Browser to localhost:8080
:
We can even use nc
to perform the HTTP request:
$ echo "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n" | nc localhost 8080 HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Server: netcat! <!doctype html> <html> <body> <h1>Hello from Netcat!</h1> </body> </html>