Small webserver

Tue, 9 Dec. 2008     Thomas Bendler     ~ 1 min to read

Do you have a shell, netcat, and some standard Unix tools available? Then you can try out this:

#! /bin/sh

:;while [ $? -eq 0 ];do
  nc -vlp 8080 -c'(r=read;e=echo;$r a b c;z=$r;while [ ${#z} -gt 2 ];do $r z;done;f=`$e $b|sed 's/[^a-z0-9_.-]//gi'`;h="HTTP/1.0";o="$h 200 OK\r\n";c="Content";if [ -z $f ];then($e $o;ls|(while $r n;do if [ -f "$n" ]; then $e "<a href="\">`ls -gh $n`";fi;done););elif [ -f $f ];then $e "$o$c-Type: `file -ib $f`\n$c-Length: `stat -c%s $f`";$e;cat $f;else $e -e "$h 404 Not Found\n\n404\n";fi)'

This is a small web server listening on port 8080 (so could be run as a normal user) which serves the files in the current directory. Pretty cool, isn’t it?

Share on: