The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-20-2008
Registered User
 

Join Date: May 2008
Posts: 68
Unhappy 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!

Last edited by MartyIX; 08-21-2008 at 04:04 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 08-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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.
Reply With Quote
  #3  
Old 08-21-2008
Registered User
 

Join Date: May 2008
Posts: 68
Unhappy

@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.
Reply With Quote
  #4  
Old 08-21-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
"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.
Reply With Quote
  #5  
Old 08-21-2008
Registered User
 

Join Date: May 2008
Posts: 68
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
Reply With Quote
  #6  
Old 08-21-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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"
The Useless Use of Test $? is also something I personally try to avoid.

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

Code:
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.

Code:
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.

Last edited by era; 08-21-2008 at 09:00 PM. Reason: Note Useless Use of Echo, too
Reply With Quote
  #7  
Old 08-22-2008
Registered User
 

Join Date: May 2008
Posts: 68
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 :-)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0