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.

 
 
» You might also be interested in the following articles:
Comparing Bash with… Windows command line shell…
Tiny bash scripts: check Internet connection availability
Simple way to transmit files over network (nc utility)
Sort CLI output by line length
Site of the day: Free Linux eBooks



» Want to stay up to date? Subscribe to our E-MAIL or RSS feed!

10 Responses to “Web server oneliner with bash”


  1. 1 Erdem Güven

    I had added directory browse support:

    :;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#^/##g;s#/$”"##g”`;h=”HTTP/1.0″;o=”$h 200 OK\r\n”;c=”Content”;if [ -z "$f" -o -d "$f" ];then($e $o;$e “Parent Directory“;if [ $f ];then f=$f/;fi;cd “$f”;ls|(while $r n;do if [ -e "$n" ]; then $e “`ls -ghd $n`“;fi;done););cd ->/dev/null;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 $f\n”;fi)’;done

    Tested on Ubuntu 8.04.

  2. 2 Erdem Güven

    Ooops, system removed all tags in the code. Here is another try:

    :;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#^/##g;s#/$”"##g”`;h=”HTTP/1.0″;o=”$h 200 OK\r\n”;c=”Content”;if [ -z "$f" -o -d "$f" ];then($e $o;$e “Parent Directory“;if [ $f ];then f=$f/;fi;cd “$f”;ls|(while $r n;do if [ -e "$n" ]; then $e “`ls -ghd $n`“;fi;done););cd ->/dev/null;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 $f\n”;fi)’;done

  3. 3 Erdem Güven

    I’m sorry for repeat comments. This must be displayed correctly:
    :;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#^/##g;s#/$""##g"`;h="HTTP/1.0";o="$h 200 OK\r\n";c="Content";if [ -z "$f" -o -d "$f" ];then($e $o;$e "<a href=\"/`dirname "$f"`\">Parent Directory</a><br>";if [ $f ];then f=$f/;fi;cd "$f";ls|(while $r n;do if [ -e "$n" ]; then $e "<a href=\"/$f$n\">`ls -ghd $n`</a><br>";fi;done););cd ->/dev/null;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 $f\n";fi)’;done

  4. 4 artiomix

    Hi, Erdem, you are welcome to send the code to me (artiomixATgmailDOTcom) and I will publish it here :)

  5. 5 dani

    hello,i am trying the version with directories,but it doesnt work
    thanks

  6. 6 Victor

    Hi Erdem. Can you post the code to pastebin, please? :)

  7. 7 ann

    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

  8. 8 troppo caffe` poco cervello

    WEBSERVER MORELINER WITH BASH

    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

  9. 9 TheWorm

    Good work Erdem Güven! But you didn’t add just the directory browse support, now you can see every file you have on you computer, not just the ones you put in the same folder where the script is.
    Try looking at http://localhost:8080///etc/passwd
    This should give you an idea about what I’m trying to explain…

  1. 1 Simple way to transmit files over network (nc utility) | Linux Screw

Leave a Reply




Friendly Sites:Who is behind Linux Screw?
Aspiring Sysadmin | GeekyBits³ | Bash Cures Cancer | TOTMS
Linux Operating System | Small Linux Deployments | My SysAd Blog
The Danesh Project | ZEPY | Linux config Wiki | Planet Sysadmin
The Sys Admin | {buhay sysad} | a non-geek's linux notes
Linux HOWTOs, Tutorials & Projects with Adam Palmer | LinuxAlt.Com
My name is Artem Nosulchik (artiomix AT gmail DOT com) and I'm Linux/Unix, Cisco systems engineer. The main idea of Linux Screw is to share relevant knowledge, skills and observations over The Web. Here you can find a lot of information related to different Linux distributions, FreeBSD, IOS as well as a other Open Source around staff. Read more ››