I want to open a port and listen for a command, and when that command comes in, immediately run a script.
The best I've come up with is (the echo is a substitute for my script):
nc -lk 9999 | grep -q runcommand && echo "running command"
, then I run this to test:
echo runcommand | nc localhost 9999
Unfortunately it only seems to echo the text on the second time I send "runcommand"??
How can I make grep exit immediately, so my script will run immediately?
nc.openbsd(packagenetcat-openbsd) if I remove the-koption-k, thencwill exit if the wrong command comes in.-kit just stays alive even when thegrepexits... Now I'm wondering if there's differences between the variants and versions of netcat. I have two on my Debian,nc.openbsdandnc.traditional, I'm not sure if there's a third one too. But.traditionaldoesn't seem to have-k, so yours can't be that.