Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

netcat

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-20-2008
MartyIX MartyIX is offline
Registered User
 
Join Date: May 2008
Last Activity: 28 February 2010, 8:51 AM EST
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Data 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 08:04 AM..
Sponsored Links
    #2  
Old Unix and Linux 08-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 08-21-2008
MartyIX MartyIX is offline
Registered User
 
Join Date: May 2008
Last Activity: 28 February 2010, 8:51 AM EST
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Data

@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 08:04 AM..
    #4  
Old Unix and Linux 08-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
"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.
Sponsored Links
    #5  
Old Unix and Linux 08-21-2008
MartyIX MartyIX is offline
Registered User
 
Join Date: May 2008
Last Activity: 28 February 2010, 8:51 AM EST
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old Unix and Linux 08-22-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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-22-2008 at 01:00 AM.. Reason: Note Useless Use of Echo, too
Sponsored Links
    #7  
Old Unix and Linux 08-22-2008
MartyIX MartyIX is offline
Registered User
 
Join Date: May 2008
Last Activity: 28 February 2010, 8:51 AM EST
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
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 :-)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with sed to add delimiter to send HEX with netcat patx Shell Programming and Scripting 3 12-30-2011 11:16 AM
Post using nc(netcat) ajaypadvi Shell Programming and Scripting 3 07-27-2011 09:04 AM
Thank you radoulov for your help on netcat command (nc -lp) vectrum Linux 0 07-02-2011 08:00 AM
Netcat with Authentication? pileofrogs UNIX for Advanced & Expert Users 1 12-04-2006 10:48 AM
netcat like file transfer linuxdba Programming 2 04-28-2005 07:10 AM



All times are GMT -4. The time now is 09:58 AM.