Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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.

share|improve this question

marked as duplicate by jw013, slm Nov 16 '14 at 4:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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
share|improve this answer

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.