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. )