Register
It is currently Wed Oct 22, 2014 7:39 pm

Simple nc UDP remote control.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Wed Sep 15, 2010 8:13 pm   

Joined: Wed Sep 15, 2010 8:09 pm
Posts: 3
I'm trying to create a simple UDP remote server.

I'd like to use nc inside a bash script, something like this:

while true; do
woot = $(nc -lu 1234)
if [ $woot = "E" ]; then
do something
else
do nothing
fi
done

I'm just a little newb-ish on parsing the stdout from things like nc, and especially whether to use the $() as above or some other syntax.

Any help (even rtfm [with a link]) would be appreciated!


Top
 Profile  
 PostPosted: Wed Sep 15, 2010 10:36 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
What you had written down was already pretty much working pseudo code :)
I did change the udp part from netcat to tcp, just because I couldn't get it to work in the 5 minutes I tried.
Code:
#!/bin/bash

while [ 1 ]; do
  woot=$(nc -lp 1234)
  if [ "$woot" = "E" ]; then
    echo "Received an E"
  else
    echo "Did not receive an E"
  fi
done

You can start that up and test it with:
Code:
for i in A B C D E F; do echo "$i" >/dev/tcp/localhost/1234; sleep 0.2; done


Top
 Profile  
 PostPosted: Thu Sep 16, 2010 7:05 am   

Joined: Wed Sep 15, 2010 8:09 pm
Posts: 3
Yes, thanks, that worked. However, I still need to figure out a UDP version.

While fooling around with your script, I noticed that it didn't work with:

user$ nc 127.0.0.1 1234
>F
>G
>E

-- which is how I've been opening "Sessions" to test my scripting. Didn't work, that is, until i escaped that line with an alt-C. At that time, The "Server" script spit out the appropriate responses. Conversely, I tried your script with the addition of -u arg to nc to see if udp would work. I couldn't get it to work with the above method (nc -u 127....), or by echoing to /dev/udp/host/port.


Top
 Profile  
 PostPosted: Thu Sep 16, 2010 11:47 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
cupofnestor wrote:
While fooling around with your script, I noticed that it didn't work with:

user$ nc 127.0.0.1 1234
>F
>G
>E

That's right, woot=$(nc -l 1234) basically means to put the (entire) output of the netcat command in variable 'woot'.
The netcat command is only complete when it is done (terminated). Then and only then, the variable 'woot' is filled with the entire output of netcat.

I had the same issue with UDP as you.


Top
 Profile  
 PostPosted: Thu Sep 16, 2010 4:41 pm   

Joined: Wed Sep 15, 2010 8:09 pm
Posts: 3
Quote:
I had the same issue with UDP as you.

Did you solve them?

What you say regarding terminating netcat makes sense, especially since the script functions identically after the while loop is removed.
I have to believe there's is a rather easy solution to this.
Is there no way to store the output of nc in a variable and parse it for linefeed at the same time?

can the output of nc be piped to awk or sed to parse each character and put it in a variable?

#!/bin/bash
nc -l 1234 | awk '{print $1 " =message received" }' ' <-- this works, prints each line, and $1 is always the last string sent and returned, I just don't know where to go from here.


Top
 Profile  
 PostPosted: Sun Jul 28, 2013 5:18 pm   

Joined: Sun Jul 28, 2013 5:06 pm
Posts: 1
I think i found a simple way to make it work with udp as well as tcp connections.

Code:
#!/bin/bash

while read -r line
do
        if [ $line = "E" ]; then
                echo "Received an E"
        else
                echo "Did not receive an E"
        fi
done < <(nc -lup 1234)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP