Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

runshell script for each netcat output 1

Status
Not open for further replies.

geebers

Technical User
May 29, 2009
20
US
is there an efficient way to fire off a shell script each time netcat receives new text string from a network port?

i need root to run myscript.sh each time a new string of text is received from port 3333. also, capture the string as a variable in myscript.sh.

thanks for any help.
m
 
Hi

Depends on the [tt]netcat[/tt] implementation you use. And also depends on your exact task. Generally would be better to run myscript.sh once and let it handle all the output :
Code:
netcat -l -p 3333 -e myscript.sh

[gray]# or[/gray]

netcat -l -p 3333 | myscript.sh
But you really want to process each line of data with another process :
Code:
netcat -l -p 3333 | while read str; do myscript.sh "$str"; done
Additionally I would take a look at [tt]inetd[/tt], [tt]xinetd[/tt] or other internet super-servers.


Feherke.
 
hmmm, I think xinetd is what i'm after. I just want to run myscript.sh when some text is sent to port 3333, using UDP.

so i installed xinetd from my repos and added the following line to /etc/services
myscript 3333/udp

created the file /etc/xinetd.d/myscript and it contains...

service myscript
{
port = 3333
socket_type = dgram
wait = no
user = fred
server = /home/fred/myscript.sh
log_on_success += USERID
log_on_failure += USERID
disable = no
}

Then I restart xinetd.

Then open port 3333 on firewall.
Now if I use netcat to send some text, myscript.sh does not run. My netcat command is....
sudo nc -u 192.168.2.321 3333

and then I type in text from stdin. But myscript.sh does not run. :(

Any help?
 
Is there any particular reason why you are using UDP?

If you add an entry to /etc/services called "myscript 3333/udp" it should work. You should have received some error messages from xinetd in your system log.

Annihilannic.
 
i have a digital signage device that uses upd to communicate.

I got it working. To use udp for a service, there are two required lines in /etc/xinetd.d/myscript ....

socket_type = dgram
wait = yes

default settings are:
socket_type = stream
wait = no

So here's my working /etc/xinetd.d/myscript file

service myscript
{
port = 3333
socket_type = dgram
wait = yes
user = fred
server = /home/fred/myscript.sh
log_on_success += USERID
log_on_failure += USERID
disable = no
}

thanks feherke for showing me the right tool to use!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top