Home » Distros » Debian » Web Server On Bash In One Line

Web server oneliner with bash

It’s amazing but it’s possible to write little web server on bash shell script. Here is it’s source code:

:;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 "`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)';done

Bash shell script web server

You can download it here.

Just run it and then load web page http://192.168.0.7:8080, where 192.168.0.7 is IP address of the Linux machine you’ve started above mentioned bash script. You’ll see links to files located in directory the script was started at. My respect to Alexey Sveshnikov.

P.S. Script was tested at Ubuntu and Debian Linux.

SHARE:
Photo of author
Author
My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

5 thoughts on “Web server oneliner with bash”

  1. it doesnt work to me ,it load and when i conect with firefox only appears a white screen 🙁

    Also the erdem code,but it shows this:
    syntax error near unexpected token `(‘

    my system :debian etch.
    some help?
    thanks

    Reply
  2. Apparently doesn’t work any more: nc no longer has -c on Ubuntu, and -l and -p are not allowed on the same call.

    Reply

Leave a Comment