Minimal web server using netcat

后端 未结 14 784
借酒劲吻你
借酒劲吻你 2020-12-04 05:15

I\'m trying to set up a minimal web server using netcat (nc). When the browser calls up localhost:1500, for instance, it should show the result of a function (date

14条回答
  •  春和景丽
    2020-12-04 05:52

    Donno how or why but i manage to find this around and it works for me, i had the problem I wanted to return the result of executing a bash

    $ while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; sh test; } | nc -l 8080; done
    

    NOTE: This command was taken from: http://www.razvantudorica.com/08/web-server-in-one-line-of-bash

    this executes bash script test and return the result to a browser client connecting to the server running this command on port 8080

    My script does this ATM

    $ nano test
    
    #!/bin/bash
    
    echo "************PRINT SOME TEXT***************\n"
    echo "Hello World!!!"
    echo "\n"
    
    echo "Resources:"
    vmstat -S M
    echo "\n"
    
    echo "Addresses:"
    echo "$(ifconfig)"
    echo "\n"
    
    
    echo "$(gpio readall)"
    

    and my web browser is showing

    ************PRINT SOME TEXT***************
    
    Hello World!!!
    
    
    Resources:
    procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
     0  0      0    314     18     78    0    0     2     1  306   31  0  0 100  0
    
    
    Addresses:
    eth0      Link encap:Ethernet  HWaddr b8:27:eb:86:e8:c5  
              inet addr:192.168.1.83  Bcast:192.168.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:27734 errors:0 dropped:0 overruns:0 frame:0
              TX packets:26393 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:1924720 (1.8 MiB)  TX bytes:3841998 (3.6 MiB)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    
    GPIOs:
    +----------+-Rev2-+------+--------+------+-------+
    | wiringPi | GPIO | Phys | Name   | Mode | Value |
    +----------+------+------+--------+------+-------+
    |      0   |  17  |  11  | GPIO 0 | IN   | Low   |
    |      1   |  18  |  12  | GPIO 1 | IN   | Low   |
    |      2   |  27  |  13  | GPIO 2 | IN   | Low   |
    |      3   |  22  |  15  | GPIO 3 | IN   | Low   |
    |      4   |  23  |  16  | GPIO 4 | IN   | Low   |
    |      5   |  24  |  18  | GPIO 5 | IN   | Low   |
    |      6   |  25  |  22  | GPIO 6 | IN   | Low   |
    |      7   |   4  |   7  | GPIO 7 | IN   | Low   |
    |      8   |   2  |   3  | SDA    | IN   | High  |
    |      9   |   3  |   5  | SCL    | IN   | High  |
    |     10   |   8  |  24  | CE0    | IN   | Low   |
    |     11   |   7  |  26  | CE1    | IN   | Low   |
    |     12   |  10  |  19  | MOSI   | IN   | Low   |
    |     13   |   9  |  21  | MISO   | IN   | Low   |
    |     14   |  11  |  23  | SCLK   | IN   | Low   |
    |     15   |  14  |   8  | TxD    | ALT0 | High  |
    |     16   |  15  |  10  | RxD    | ALT0 | High  |
    |     17   |  28  |   3  | GPIO 8 | ALT2 | Low   |
    |     18   |  29  |   4  | GPIO 9 | ALT2 | Low   |
    |     19   |  30  |   5  | GPIO10 | ALT2 | Low   |
    |     20   |  31  |   6  | GPIO11 | ALT2 | Low   |
    +----------+------+------+--------+------+-------+
    

    simply amazing!

提交回复
热议问题