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.
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
Script
Client
h/t to this the answer here, which I incorporated into the above.
https://stackoverflow.com/questions/6269311/emulating-netcat-e