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

PC1:~$ nc -l 9999

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

PC2:~$ nc 9999
    what's up

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.

  • 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
    Commented Nov 10, 2014 at 23:49

2 Answers 2


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

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.

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