![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Netcat with Authentication? | pileofrogs | UNIX for Advanced & Expert Users | 1 | 12-04-2006 07:48 AM |
| netcat like file transfer | linuxdba | High Level Programming | 2 | 04-28-2005 03:10 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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! Last edited by MartyIX; 08-21-2008 at 04:04 AM. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
@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)
Last edited by MartyIX; 08-21-2008 at 04:04 AM. |
|
#4
|
|||
|
|||
|
"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.
|
|
#5
|
|||
|
|||
|
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 :-( ================================================ 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 |
|
#6
|
|||
|
|||
|
On the face of it, looks okay. You could probably avoid at least some amount of temporary files by using pipelines instead.
Code:
DO_SOME_STUFF | nc -w 1 "$remote_host" "$port" Code:
if echo "$data|$thisMachine" | nc -w 1 "$host" "$port" then # wait for data nc -l -p "$port" >"output.file"; fi Code:
echo "$data|$thisMachine" | nc -w 1 "$host" "$port" && nc -l -p "$port" >"output.file" Code:
thisMachine=$(/sbin/ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1) 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. Last edited by era; 08-21-2008 at 09:00 PM. Reason: Note Useless Use of Echo, too |
|
#7
|
|||
|
|||
|
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 :-) |
|||
| Google The UNIX and Linux Forums |