0

Shell Web Server :: Netcat is neat

A very basic bash web server. Netcat is pretty epic. Referenced Paul Buchheit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
 
HOMEDIR=./
RESP=/tmp/webresp
[ -p $RESP ] || mkfifo $RESP
 
while true ; do
( cat $RESP ) | nc -l 8000 | (
REQ=`while read L && [ " " "<" "$L" ] ; do echo "$L" ; done`
URL=`echo $REQ | head -1 | awk -F" " '{print $2}'`
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $REQ" | head -1
 
index='index.html'
if [ "$URL" == "/" ]; then	
	file=index.html
else
	file=$URL
fi
 
if [ ! -f $HOMEDIR$file ]; then
	CODE=400
	CONTENT='Not Found'
else
	CODE=200
	CONTENT=`cat $HOMEDIR$file`
fi
 
cat >$RESP <<EOF
HTTP/1.1 $CODE OK
Cache-Control: private
Content-Type: text/plain
Server: bash/3.2.48
Connection: Close
Content-Length: ${#CONTENT}
 
$CONTENT
EOF
)
done

Leave a Reply

Spam protection by WP Captcha-Free