0

(I have been testing (trying) like a thousand posts like chats, web servers, server-clients for netcat, no luck so far, therefore thinking if is it possible at all)

Lets say I have a silly bash function that all that does is to reply you saying you hello and how long is your name, and a script, on interactive console input so users can fell good (./myScript.sh interactiveBash):

#!/bin/bash

responseTo(){
    local output=$1
    local input=$2

    plentyOfThings="Hello $input, you name is ${#input} characters long, and youre gona have a great day"
    ##may take a while, on tests, up to 3-5 seconds

    eval $output="'$plentyOfThings'"
    }


if [ "${1}" == "interactiveBash" ]
    then
        echo -n "What's you name? "
        while read; do

            response=""
            responseTo response ${REPLY}
            echo "··························· ${response}"

            echo -ne "\nWhat's you name? "

            done
    fi

Cool, I run in a terminal, user can put his name, and terminal say hello and wish them a great day

now, becouse how that pretty complex function (responseTo) count the lenght of the name is pretty secret, need to be on a remote computer

so I tought to extend my scritp with (./myScript.sh onlineNetcatServer):

if [ "${1}" == "onlineNetcatServer" ]
    then
        my_port=8080
        echo "Listening at ${my_ip}, and aying hello"

        while true; do

            nc -kl ${my_port} -w 0 \
                0< < .... \
                1> > .... \
                2> ....  #errors

            ....

            echo "${_input} was here on IP ${_IP} and I told him ${_resposne}"
            
            done
    fi

and then give user the following script (./myScript.sh 192.168.1.42)

    remome_ip=${1}
    remote_port=8080

    echo -n "What's you name? "

    while read; do

        response=""

        nc -kl ${remome_ip} ${remote_port} -w 0 \
            0< < ....${REPLY}.... \
            1> > ....${response}.... \
            2> ....  #errors

        ....

        echo "··························· ${response}"
        
        done

sure, all comands in betwwen dots are missing, and no idea how to continue

(like the server will "serve" multiple clients, the netcat session need to be closed as soon as response is given, and preferiable, keep other waiting until served)

is that even possible with this estructure / usage of NetCat ??

PedroNG
  • 21
  • 4
  • `eval $output="'$plentyOfThings'"` Read about namereferences. There are many, many implementations of http server in bash. Research them, they do the same thing. [For example](https://stackoverflow.com/a/24342101/9072753) that script looop the netcat output to itself back again using a fifo. But personally, [`socat` is just simpler](https://stackoverflow.com/a/30277125/9072753). – KamilCuk Dec 03 '20 at 13:25
  • thanks for references, tryting to understand them ... (not easy), – PedroNG Dec 04 '20 at 12:02
  • and for socat. in the example, the line [ socat TCP4-LISTEN:${LISTEN_PORT},fork EXEC:"${0}] what really is doing is calling the script again (jn a separate process I asume) to this new process stablis the conection, doing in that sense the "multi-client", isn0t it ? – PedroNG Dec 04 '20 at 12:04
  • `isn0t it ?` Yes. – KamilCuk Dec 04 '20 at 12:56
  • thanks a lot, I starting to get the usefulness of socat – PedroNG Dec 10 '20 at 09:35

0 Answers0