1

I want to run a shell script, that sends a packet to localhost (loopback) when it receives a packet from anywhere.

I am using netcat.

nc -lp $port, and then echo test | nc localhost $port works.

But, the same thing inside a shell script, that can execute code when receiving a package, does not.

nc -lp $port | ./a.sh and then the following script:

#!/bin/bash
while read socket
do
    echo test | nc localhost $port
done

It gives the error:

localhost [127.0.0.1] 2012 (?) : Connection refused

There is probably a simple explanation to why.

I wonder: is it possible to do what I want to do, somehow?

Why I want to do this: Writing a simple network application that can either talk between nodes, or between accounts on the same node, and I want to use the same code for both (thus interaction between accounts on the same node is via loopback messages instead of just directly. )

1 Answer 1

1

OK. I have resolved the question now. It was multiple things that caused the error.

First, netcat does not allow multiple connections (see https://stackoverflow.com/a/29780420/19923651 for reference. )

I resolved that by using socatinstead.

Then there were some other issues too, that made me misinterpret my attempts at debugging.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .