1

I have been learning nc for a while now, and I decided to make a social thread server for it in bash. Surprisingly, I got it working, but I have a couple problems with it. My main problem I want to solve here is how to allow only one connection at a time, through sending the thread and receiving comments for the thread. Since I have to add a delay between sending the thread and receiving comments, two or more clients can try and receive and send at the same time, and this breaks the server. I would like to allow it where when one client requests the thread, it sends it without allowing anyone else being able to connect. Another problem is compatibility, because different versions of netcat exist. I developed this on my laptop which had BSD nc, and when I tested it on my pc, it did not work the same as it did on my laptop because of it having a different version of `nc', and made the server become stuck. If there are any ways to fix this, or any alternate ways to achieve what I want, it would be really helpful to me.

Code for the client:

#!/bin/bash

if [ "$1" == "" ]
    then
    echo "No IP selected."
    exit 0
fi

if [ "$2" == "" ]
then
        echo "No port selected."
        exit 0
fi

if [ ! -e ~/.netthread ]
then
    mkdir ~/.netthread
    mkdir ~/.netthread/threads/
    touch ~/.netthread/config
    echo -n "Username: "
    read name
    echo $name >~/.netthread/config
fi

export ip=$1

export port=$2

getthread ()
{
    echo "Getting thread..."
    nc $ip $port >~/.netthread/threads/$ip:$port
    sleep 1
    nc $ip $port </dev/null
    echo "Thread obtained!"
}

readthread ()
{
    cat -s ~/.netthread/threads/$ip:$port | less +G
}

writethread ()
{
    wow="$(cat ~/.netthread/config)"
    touch /tmp/thread$$
    chmod 700 /tmp/thread$$
    echo "==============================" >> /tmp/thread$$
    echo "User: $wow" >> /tmp/thread$$
    echo -n "Date: " >> /tmp/thread$$
    date >> /tmp/thread$$
    echo "Press Ctrl-D to stop writing"
    cat -s >> /tmp/thread$$
    echo -e "\n" >> /tmp/thread$$
    echo "==============================" >> /tmp/thread$$
    echo "Sending comment..."
    nc $ip $port >/dev/null
    sleep 1
    nc $ip $port </tmp/thread$$
    echo "Comment sent!"
}

getthread
readthread

echo -n "Would you like to add a comment?(y/n): "
read lolz

if [ "$lolz" == "n" ]
then
    exit 0
else
    writethread
    echo -n "Would you like to overview?(y/n): "
    read over
    if [ "$over" == "y" ]
    then
        getthread
        readthread
    fi
    rm /tmp/thread$$
    exit 0
fi

Code for server (yes, it's stupid):

#!/bin/bash

while [ 1 ]
do
    echo "send state"
    sudo nc -l 22 < thread
    echo "recive state"
    sudo nc -l 22 >> thread
done
  • 1
    ncat can ... Act as a connection broker, allowing two (or far more) clients to connect to each other through a third (brokering) server. This enables multiple machines hidden behind NAT gateways to communicate with each other, and also enables the simple Ncat chat mode. It's also fairly well maintained, unlike your typical nc. – mikeserv Jan 11 '16 at 22:17
  • /tmp/thread$$ is bad, especially on multi-user systems, on account of the various local security flaws thus created (arbitrary file overwrite against the user running the code, for example). – thrig Jan 11 '16 at 22:28
  • @thrig Don't worry, this is on a Raspberry Pi. – SpecialBomb Jan 11 '16 at 23:00
  • @mikeserv Does ncat act like this by default, or are there any options I need to add? – SpecialBomb Jan 11 '16 at 23:19
  • @SpecialBomb - the docs are linked above. You have to use a switch, or else the -chat or whatever switch uses connection brokering to set up a chat server automatically. – mikeserv Jan 12 '16 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.