New Netcat server script

Posted on October 7th, 2006 by dandavis

In this post, I gave a command to serve up a Mozilla search plugin and icon file so you could use a JavaScript call to add the search plugin without restarting Firefox… that makes more sense if you read the other article…

Well, I decided to put that code into a bash script in case I needed it again… enjoy. Or, not.

#!/bin/bash

FILE=$1
PORT=$2

if [[ "x" == "x${FILE}" || "x" == "x${PORT}" ]]; then
        echo "error: server <filename> <port>"
        exit
fi

if [[ "x" == "x${UID}" ]]; then
        UID=`id -u`
fi

if [[ ${PORT} < 1024 && ${UID} > 0 ]]; then
        echo "error: unable to use privileged port"
        exit
fi

(echo "HTTP/1.1 200 OK" ; \\
 echo "Content-Length: "`wc -c ${FILE} | \\
      awk '{print $1}'` ; \\
 echo "Connection: close" ; \\
 echo ; cat ${FILE} ) | nc -l -p ${PORT}

Post a comment