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.

 

21 Comments

 

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

  2. January 29, 2009  7:24 pm by Erdem Güven Reply

    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 OKrn";c="Content";if [ -z "$f" -o -d "$f" ];then($e $o;$e "<a>Parent Directory";if [ $f ];then f=$f/;fi;cd "$f";ls|(while $r n;do if [ -e "$n" ]; then $e "<a>`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 Foundnn404 $fn";fi)';done



    Tested on Ubuntu 8.04.

  3. January 29, 2009  7:30 pm by Erdem Güven Reply

    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 OKrn";c="Content";if [ -z "$f" -o -d "$f" ];then($e $o;$e "<a>Parent Directory";if [ $f ];then f=$f/;fi;cd "$f";ls|(while $r n;do if [ -e "$n" ]; then $e "<a>`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 Foundnn404 $fn";fi)';done

  4. January 30, 2009  11:36 am by Erdem Güven Reply

    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 OKrn";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 Foundnn404 $fn";fi)';done

  5. January 30, 2009  12:07 pm by artiomix Reply

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

  6. May 24, 2009  11:52 am by dani Reply

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

  7. May 24, 2009  9:19 pm by Victor Reply

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

  8. May 26, 2009  6:30 pm by ann Reply

    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

  9. October 4, 2009  1:31 am by troppo caffe` poco c Reply

    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 OKrn"

    c="Content"

    if [ -z $f ];then

    ($e $o

    ls|(

    while $r n

    do

    if [ -f "$n" ]; then

    $e "<a href="/$n" rel="nofollow">`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 Foundnn404n"

    fi

    )'

    done

  10. November 4, 2009  11:31 pm by TheWorm Reply

    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...

  11. March 28, 2010  4:16 pm by Rosena Morfee Reply

    Einen tollen Artikel hast du hier geschrieben. Dieser Artikel hat mich irgendwie animiert auch wieder mehr zu bloggen, werde mich jetzt gleich mal hinsetzen und einen neuen Posts posten.

  12. October 11, 2010  12:00 pm by Scotty Silvestri Reply

    Hello please forgive my spelling , it is not my first language. I found your article on Bookmarking site "Web server on bash (in one line!) - Linux * Screw" was more or less close to what I was researching for, but after reading through your blog post I still was not able find simple solution to my challenge and this is driving me cuckoo. I enjoy your blog and think it is without a question helpful but I think you can make it even better if you polish your blog search to make it easier for searchers to locate required information within your blog. You can get this done by adding WordPress addon… As far as I can remember addon was called advanced search it has gobs of handy options like search excerpts, search attachments. If you have any questions about configuring internal search you can contact me at Beatty87@gmail.com Thank you I hope you have a good day

  13. December 31, 2010  10:17 pm by brake lines Reply

    I just discovered your blog right now and I have to say that I am not disappointed. Keep up the fantastic writing.

  14. January 5, 2011  4:15 pm by 1 Reply

    What a great blog. I've enjoyed digesting through your site in the last week.

  15. February 9, 2011  10:36 pm by ??????? Reply

    Extremely rated post. I learn something totally new on totally different blogs everyday. Deciding on one . stimulating to learn the paper content from different writers and learn a little bit one thing from their website. I’d like to use certain of this content on my blog you’re mind. Natually I’ll give a link here we're at your internet-site. Appreciate your sharing.

  16. February 10, 2011  12:17 pm by Alva Coster Reply

    Wow..pretty informative. I'm glad I found this on ! :)

  17. March 8, 2011  3:32 pm by battery chargers Reply

    I was just looking for this information for a while. After six hours of continuous Googleing, at last I got it in your site. I wonder what is the Google's problem that does not rank this kind of informative sites closer to the top. Usually the top sites are full of garbage.

  18. March 19, 2012  12:37 pm by Steven Pemberton Reply

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

  19. April 26, 2012  5:44 am by longchamp handtaschen Reply

    There are many unique ideas in time in it all publish but I don’t understand if I observe all of them core to heart. There may be a few validity but I will bring hold point of view until I check into it further. Good writing , thanks and we want even more! Added to FeedBurner as well

  20. July 24, 2012  1:19 pm by Paolo Supino Reply

    Well it isn't strictly bash, it uses netcat...

  21. May 5, 2013  1:22 pm by can you have strep throat without tonsils Reply

    In this instance, H Flu is brief for H. Influenza and
    that is a bacterial disease that responds to antibiotics.
    The identify is baffling but I didn't decide on it.

Leave a reply

 

Your email address will not be published.