How to create a simple web server with 5 lines of bash

These days the blog has been a bit lively with the topic of servers, our friend Fico has been giving us a lecture on computer networks in his series Computer Networks for SMEs, plus a couple of articles for installing and configuring web servers in How to install and configure XAMPP on GNU / Linux y How to install NGINX with Google Page Speed ​​on Ubuntu automatically.

How the world of free software is so nurturing, sometimes simple and other times a little more complex, today we bring you a small 5-line attempt, which allows us to create a simple web server.

shinatra

shinatra is the name of the attempt made by Ben raady and that it has been freely distributed, it is as effective as it is accurate, we simply create or download the bash with the necessary lines of code and execute the bash with the port + the message that we want to host.

This script that allows us to create a simple web server, works in various GNU / Linux distros and in OS X, it does not need installation, it is flexible in the paths that you want to use, it allows all kinds of content, plus it can reach be a very useful tool, in case you want to put information quickly on any server.

How to use Shinatra

shinatra It is made up of the following lines of code:

#! / bin / bash RESPONSE = "HTTP / 1.1 200 OK \ r \ nConnection: keep-alive \ r \ n \ r \ n $ {2: -" OK "} \ r \ n" while {echo -en " $ RESPONSE "; } | nc -l "$ {1: -8080}"; do echo "============================================== = "done

You can simply create a .sh file, with whatever name you want, and then run it as follows:

./shinatra.sh [port] [response]

You can also get the updated script from the official repositories, to do this, follow these steps:

git clone https://github.com/benrady/shinatra.git cd shinatra / ./shinatra.sh 80 'Server Reply Message'

To the script you can send whatever you want, for example:

Create a simple web server

Without a doubt, a fast, simple and easy way to put a web server to work, the utility and scope of it is varied. What use would you make of this script? Do you find it useful?


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Gaspar Fernandez said

    Some time ago I made a similar script in which you could define several answers according to the route and everything. It was not 5 lines but we could do several things: http://totaki.com/poesiabinaria/2015/03/mini-servidor-web-con-bash-y-netcat-para-paginas-en-mantenimiento/

    The bad thing about doing it netcat style is that we don't take advantage of concurrency, and resources are wasted a lot, but it's a nice experiment 🙂

  2.   Gonzalo Martinez said

    Another way is with python (which comes in practically all distros)

    sudo python -m SimpleHTTPServer 80

    It works differently, this way serves the files that are in the directory where the server is running, but it is another simple way

  3.   jose perez said

    another with php is
    php -S 127.0.0.1:9000

  4.   Antonio Cifuentes placeholder image said

    I read this post a year ago, in case you want a complete list of command line web servers.

    https://www.busindre.com/servidor_web_por_linea_de_comandos.

  5.   Edward Cuomo said

    You can transfer a file on a server that doesn't have anything installed, and you don't have ROOT permission:

    #! / Bin / bash
    file = »$ 1 ″
    port = 8080

    n = »\ r \ n»
    body = »$ (base64« $ {file} »)»

    RESPONSE = »HTTP / 1.1 200 OK»
    RESPONSE = »$ {RESPONSE} $ {n} Connection: keep-alive»
    RESPONSE = »$ {RESPONSE} $ {n} Content-Type: application / octet-stream»
    RESPONSE = »$ {RESPONSE} $ {n} Content-Disposition: inline; filename = \ »$ {file} \» »
    RESPONSE = »$ {RESPONSE} $ {n} $ {n} $ {body}»

    while {echo -en "$ RESPONSE"; } | nc -l "$ {port}"; do
    echo «============================================== »
    done

    Then, when you receive it, you transform it again with "base64 -d filename"

  6.   Edward Cuomo said

    There are many more examples in:
    $ man nc

  7.   Jose Maria Garcia said

    On Linux and macOS, the preferred shell and package manager can be used. On Windows 10, you can install Windows Subsystem for Linux to get a version of Ubuntu and Bash integrated with Windows, very good tutorial.

    https://clongeek.com/como-instalar-macos-desde-usb/