netcat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting netcat
# 1  
Old 08-20-2008
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..
# 2  
Old 08-21-2008
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  
Old 08-21-2008
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 08-21-2008
"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  
Old 08-21-2008
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  
Old 08-22-2008
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
# 7  
Old 08-22-2008
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 :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Netcat ( nc -l ) as webserver

Dear Linux guru's I am trying to create a webserver using nc (netcat only) on RHEL 7.2 running on bash shell. now the easy thing is to get nc listing to a port and respond back $ while true; do { echo -e 'HTTP/1.0 200 OK\r\n'; set; } | nc -l 7877; done This when called from a... (3 Replies)
Discussion started by: chakrapani
3 Replies

2. UNIX for Advanced & Expert Users

Telnet vs netcat behavior

Currently I run a number of network tests using netcat that checks for an open port on a remote IP-address, using this syntax: netcat -v -w 5 -z 107.249.95.5 4488 For some reason, the netcat command above is hanging (although others work fine), but a telnet is showing a valid connection like... (4 Replies)
Discussion started by: ckmehta
4 Replies

3. IP Networking

Help with Netcat

Hi all, I know my question is regarding Windows and not Linux, but I simply need people who know Netcat pretty well and I'm guessing here is a good place for that. So on with my question. I'm doing some research, and I was playing around with netcat on a WinXP VM but I can't seem to get... (0 Replies)
Discussion started by: MrCrumbs
0 Replies

4. IP Networking

nc (netcat): Connection refused problem

From my machine (ubuntu), I can do $ nc somemachine 80 But, when I want to connect to other machine that runs nc on different ports, it throws "Connection refused" error. The same error occurs on my local machine. For example, nc -l 5555 nc x.x.x.x 5555 --> this throws "Connection... (2 Replies)
Discussion started by: dinga
2 Replies

5. Shell Programming and Scripting

Post using nc(netcat)

Hi; I have a url like http://localhost:8080/examples/jsp/dates/nextPageToPost.jsp?name=ajay&password=pas&sex=Male&check=on&nationality=USA&description=aa&submit=submit in which i want to use nc for http post for parameters like "name","password"....etc can neone please help me how to do that... (3 Replies)
Discussion started by: ajaypadvi
3 Replies

6. Linux

Thank you radoulov for your help on netcat command (nc -lp)

Hello, Thank you very much for the line nc -lp <port> . I tried to run simple chat session with nc as it's shown in catonmatDOTorg but failed miserably with that syntax inspite of opening port 7777 by iptables . But your command example is working nicely. So a bagful of thanks :)) Only one... (0 Replies)
Discussion started by: vectrum
0 Replies

7. Solaris

Please help me to install netcat on solaris

hello guys, i want to install netcat on my solaris. after i tar and gunzip netcat i'm confuse what do i must to do ? please help me to install netcat on my solaris. I'm beginner :( (2 Replies)
Discussion started by: praset
2 Replies

8. UNIX and Linux Applications

netcat prints blank pages

Please direct me to the right forum tree if i am in the wrong section for this. i have netcat on a unix machine and there is no man nc or man netcat available. my command i am using is: cat $FILE1 | netcat -h $PRINTER -p 9100 (-h -p -d are the only flags available in this version of... (3 Replies)
Discussion started by: dunpealslyr
3 Replies

9. UNIX for Advanced & Expert Users

Netcat with Authentication?

I'd like to do a data transfer without encryption but with a guarantee that my data comes from a legit source. I'm thinking something that uses a public key scheme to sign the data. Does anyone know of something like that? Thanks! -Pileofrogs (1 Reply)
Discussion started by: pileofrogs
1 Replies

10. Programming

netcat like file transfer

Hi Folks I am not a c programmer .But i need help in writing a program which can do this any ideas on how to go about it . i start a server on the target server where files need to be copied start-server -port 5006 & ---start the server and listen it on a partcular port on the source... (2 Replies)
Discussion started by: linuxdba
2 Replies
Login or Register to Ask a Question