0

Trying to create a simple netcat responder:

The "server" would be:

$ while true; do { ./secret.sh; } | sudo netcat -k -q -1 -l 123; done

secret.sh is:

#!/bin/bash
read line
if [ "$line" == "open" ]; then
    echo "sesame"
fi

And the client connection:

$ echo "open" | netcat localhost 123

It's not working as expected. What can be changed to make this work?

After reading some feedback elsewhere, the following changes were suggested:

mkfifo /tmp/pipe
while true; do { cat /tmp/pipe | ./secret.sh 2>&1; } | netcat -kv -lp 123 > /tmp/pipe; done

This works, but it only responds with the results from secret.sh the first time. Subsequent connections with the correct string don't get the expected response. I'm getting closer, however.

Utkonos
  • 332
  • 3
  • 12

1 Answers1

0

Figured it out. The problem was the loop should not be on the command line, it needs to be inside the shell script. The command read line blocks until it recieves data from the listening port. All it needs is to be wrapped in a loop.

Server

mkfifo pipe
nc -lk 123 0<pipe | ./secret.sh 1>pipe

Script

#!/bin/bash
while true
do
    read line
    if [ "$line" == "open" ]; then
        echo "sesame"
    fi
done

Client

echo "open" | nc localhost 123

h/t to this the answer here, which I incorporated into the above.

https://stackoverflow.com/questions/6269311/emulating-netcat-e

Utkonos
  • 332
  • 3
  • 12