Using netcat as a webserver

I had a requirement to quickly run a webserver on a certain port to allow me to access a file, but without modifying the system so I could’nt install an RPM. After some thought, and I came up with this script as an easy solution.

#!/bin/bash

trap 'my_exit; exit' SIGINT SIGQUIT

my_exit()
{
        echo "you hit Ctrl-C/Ctrl-\, now exiting.."
        # cleanup commands here if any
}

if test $# -eq 0 ; then
        echo "Usage: $0 filename"
        echo ""
        exit 1
fi

filename=${1}

while true
do
        echo -e 'HTTP/1.1 200 OK\r\n'; >> logfile 2>&1
        cat ${filename} | nc -l 2000 >> logfile 2>&1
done
Advertisements
This entry was posted in linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s