netcat

Is there a way how to react on the message a client sent to the server?

I would like as the client sent message to server: "get information such and such" and server would answer.

Thank you for reply!

The netcat distribution contains some example scripts which demonstrate this. However, you might be better off writing a simple server yourself. If you know perl, copy/paste the client/server examples from the perlipc manual page.

@era: Thank you. Are these examples on the internet? I'm unable to find them on the machine I use (it's a school one)

"quick example listen-exec server" - Google Search is what I primarily had in mind. The sites which have this file also most likely have the other files from the netcat distribution in the same directory. (Currently only one proper hit for me -- I'd have expected dozens. Actually if you click "omitted results included" you get 11 hits.) Googling for netcat examples also gets some interesting hits, although many are unrelated to your question.

Thank you! The first one is good. I tried "netcat examples" and so on..

I worked out a solution for my task (script that behaves as finger utility) that uses netcat this way and I need to know if the solution is good or not :frowning:

Script server-finger.sh still runs and waits for requests from clients. It uses indefinite loop:

while true ; do

echo "[Waiting for request]"
echo -n "" >"request.file"
nc -l -p 40017 >"request.file" \# here it listens for requests

if [ -s "request.file" ]; then

  \# now we send information via netcat back  

  echo "[Sending output for request: $user]"
  DO\_SOME_STAFF >"output.file"
  nc -w 1 "$remote_host" "$port"<"output.file"
  echo "[Request \($user\) was served.]"

fi

done

===============================================
Client script

# retrieve IP address of this machine
thisMachine=$(echo `/sbin/ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1`)
host=$(echo $1 | cut -d'@' -f 2)
port=40017
data="SOME DATA TO SEND"

# send data
echo "$data|$thisMachine" | nc -w 1 "$host" "$port"

exitCode=$?

if [ $exitCode -eq 0 ]; then

\# wait for data
nc -l -p "$port" >"output.file";

fi

On the face of it, looks okay. You could probably avoid at least some amount of temporary files by using pipelines instead.

DO_SOME_STUFF | nc -w 1 "$remote_host" "$port"

The Useless Use of Test $? is also something I personally try to avoid.

if echo "$data|$thisMachine" | nc -w 1 "$host" "$port"
then
  # wait for data
  nc -l -p "$port" >"output.file";
fi

or even more succinctly

echo "$data|$thisMachine" | nc -w 1 "$host" "$port" && nc -l -p "$port" >"output.file"

Finally, there's a Useless Use of Echo which is kind of silly.

thisMachine=$(/sbin/ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1)

But these are merely stylistic issues. (Haven't actually tested your code, though)

My compliments for the judicious use of double quotes; they're a bit on the paranoid side when the strings do not contain any whitespace or variable interpolations, but better safe than sorry.

Thank you for comments I've already adjusted my script.
Well, I got used to double quotes from other languages and it seems to me it's kinda lucid then :slight_smile: