1

Assume I let netcat listen on some port on PC1 (IP:10.0.0.1) with:

PC1:~$ nc -l 9999

I connect from PC2 (IP:10.0.0.2) and send some strings:

PC2:~$ nc 10.0.0.1 9999
    hello
    touchit
    test
    what's up
    touchit
    bye

How do I have to modify the the first command on PC1 such that the command 'touch test.txt' is run on PC1 whenever I send 'touchit' from PC2?

It would be great to do that with a clever combinations of standard command and pipes. Of course, 'touch test.txt' could then be replaced by an arbitrary command. It would be extremely cool, if you could even launch different programs with different command strings sent from PC2.

Braiam
  • 35,380
  • 25
  • 108
  • 167
multimax
  • 11
  • 2
  • 2
    You need to execute a command interpreter from the host end, which is what SSH is meant to do. Why must you use netcat instead of SSH? – Braiam Nov 10 '14 at 23:49

2 Answers2

1

You want something like this:

echo -e "foo\ntouchit\nbar\nbaz" | while read line; do case $line in
  touchit) touch test.txt;;
  bar)     echo bar found;;
esac; done
michas
  • 21,190
  • 4
  • 63
  • 93
0

If the program shall be started once only:

nc -l 9999 | 
  { awk '/^touchit$/ {matched=1;exit(0);}; 
    END {if(matched==1) { exit(0); } else exit(1); }'
    && echo ja; }

Unfortunately nc doesn't finish immediately after the touchit.

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174