Mac OS X,  Mac OS X Server,  Mac Security

Web Server Bash One Liner for Linux or Mac

The nc (or netcat) binary is useful for a variety of TCP or UDP operations. You can open a listener, proxy connections, open a connection to another device, and port scan a device. And you can do it all through TCP and/or UDP, define ports, and scripting with nc is pretty easy.

So in the below code we’ll start a while loop and then execute an echo of a header so a browser knows how to interpret what we’re sharing, which is a cat of our file. Then we’ll pipe that into netcat with a -l option so we can define the port and end the loop.

while TRUE; do { echo -e 'HTTP/1.1 200 OK\r\n'; cat /Users/ce/Desktop/fancy.pdf; } | nc -l 8000; done

You’ll then see standard agent information about visitors to your web server as follows (From a Firefox browser running on a Mac):

Host: 10.15.43.212:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,/
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

This is of course not ssl-enabled and lacks all the fancy-schmancy of a normal website. However, if you just need to echo some text from a quick script you run into a file and then serve that up for a short period so you can get driving to a thing you’re late for, giving someone else access to something they need on your LAN then maybe you’re in luck. Just use Control-C or Control-Z to stop your web service.