The last scripted netcat server I wrote about was based on a one line webserver.
It was a hack that kind of worked in a pinch, but the blind loop made it a bad server.
This post covers a scripted netcat server that waits for the request header to terminate before sending a response.
This makes the script well behaved.
Software Versions
Instructions
A well behaved scripted JSON/HTTP date server is below.
The script executes in a temporary directory.
When the script is killed, this directory is deleted.
The temporary directory and the netcat parameters are printed when the script starts.
The request is stored in a regular file.
The response travels through a named pipe back through netcat to the client.
In theory the request could be parsed or every request could be stored in a different file.
The response is generated in a shell function so the output can easily be customized.
The request number is printed before every request executes.
Lines that come in are prepended with “< “ and echoed to the terminal.
Outgoing lines are prepended with “> “ and echoed.
Terminal output is printed and the request is sent only after a blank request line is received.
The date is calculated only after receiving the request.
date_server.sh
A single file server is like the date server.
There are only a few changes.
A file variable has been added and the content type variable has been moved.
The script gets the full path of the variable before moving into the temporary directory.
The body variable reads from a file instead of creating dynamic content.
file_server.sh
The following file can be used for testing the file server.
index.html
The following command can be used to test either server from the command line.