Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian User (http://www.linux-archive.org/debian-user/)
-   -   netcat in listen mode don't exit (http://www.linux-archive.org/debian-user/243889-netcat-listen-mode-dont-exit.html)

Paulo Brito 02-11-2009 04:25 PM

netcat in listen mode don't exit
 
I"m trying to setup netcat so it serves logs to other systems. When a client connects, the server starts to send some logs.

I run netcat in listem mode like this:

# nc -l -p 5558 -c "tail -f /var/log/syslog"




the client I run like this:

# nc <host> 5558

The problem is that the server keeps running when client disconnects. The netstat doesn't show the connection anymore, but if I try to reconnect, I get "connection refused". Already tryed -k and -q 0 options (but I dont know if I understood them correctly).



The problem seams to be in the -c option. Without that, all works perfectly.

Any help would be apreciated.

Michael Iatrou 02-11-2009 05:15 PM

netcat in listen mode don't exit
 
When the date was Wednesday 11 February 2009, Paulo Brito wrote:

> I"m trying to setup netcat so it serves logs to other systems. When a
> client connects, the server starts to send some logs.
>
> I run netcat in listem mode like this:
>
> # nc -l -p 5558 -c "tail -f /var/log/syslog"

while :; do nc -l -p 5558 -c "tail -f /var/log/syslog"; done

--
Μιχάλης Ιατρού (txzu)


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Paulo Brito 02-11-2009 05:30 PM

netcat in listen mode don't exit
 
Michael, thanks for your reply. But if you read my entire email, you'll see that the problem is netcat NOT exiting after disconnect. So, your script will not solve the problem, because the nc never exits.

Thanks anyway.


2009/2/11 Michael Iatrou <m.iatrou@freemail.gr>

When the date was Wednesday 11 February 2009, Paulo Brito wrote:



> *I"m trying to setup netcat so it serves logs to other systems. When a

> client connects, the server starts to send some logs.

>

> I run netcat in listem mode like this:

>

> # nc -l -p 5558 -c "tail -f /var/log/syslog"



while :; do nc -l -p 5558 -c "tail -f /var/log/syslog"; done



--

*Μιχάλης Ιατρού (txzu)





--

To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org

with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Paulo Brito 02-11-2009 05:59 PM

netcat in listen mode don't exit
 
It should, but it doesn't. It only made diference when there's no connection. When a connection is made, even with -w 5 (for instance), nc waits forever.

2009/2/11 Sudev Barar <sbarar@gmail.com>

2009/2/12 Paulo Brito <paulorsbrito.listas@gmail.com>:


> Michael, thanks for your reply. But if you read my entire email, you'll see

> that the problem is netcat NOT exiting after disconnect. So, your script

> will not solve the problem, because the nc never exits.

>



Will -w option work?



--

Regards,

Sudev Barar

Read http://blog.sudev.in for topics ranging from here to there.



PS: I know most of people do not follow email niceties (mostly they

are not aware) but if you follow bottom post/in-line post style of

email conversations it becomes a whole lot easier to carry on

meaningful dialogue and you can snip out what is not meaningful too.

Most people just hit reply button and top post leaving prior message

appended uselessly at bottom. See if you can adopt this style and

persuade others. In case you are already doing this ..... great,

spread the message.

Michael Iatrou 02-11-2009 06:05 PM

netcat in listen mode don't exit
 
When the date was Wednesday 11 February 2009, Paulo Brito wrote:

> 2009/2/11 Michael Iatrou <m.iatrou@freemail.gr>
>
> > When the date was Wednesday 11 February 2009, Paulo Brito wrote:
> > > I"m trying to setup netcat so it serves logs to other systems. When
> > > a client connects, the server starts to send some logs.
> > >
> > > I run netcat in listem mode like this:
> > >
> > > # nc -l -p 5558 -c "tail -f /var/log/syslog"
> >
> > while :; do nc -l -p 5558 -c "tail -f /var/log/syslog"; done
> >
> Michael, thanks for your reply. But if you read my entire email, you'll
> see that the problem is netcat NOT exiting after disconnect. So, your
> script will not solve the problem, because the nc never exits.

You execute ``tail -f', which actually never returns. You may try the -w
delay option:

# while :; do nc -l -w 10 -p 5558 -c "tail -f /var/log/syslog"; done

Please avoid top posting.

Thanks,

--
Μιχάλης Ιατρού (jbkl)


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Paulo Brito 02-11-2009 06:46 PM

netcat in listen mode don't exit
 
2009/2/11 Michael Iatrou <m.iatrou@freemail.gr>

When the date was Wednesday 11 February 2009, Paulo Brito wrote:



> 2009/2/11 Michael Iatrou <m.iatrou@freemail.gr>

>

> > When the date was Wednesday 11 February 2009, Paulo Brito wrote:

> > > *I"m trying to setup netcat so it serves logs to other systems. When

> > > a client connects, the server starts to send some logs.

> > >

> > > I run netcat in listem mode like this:

> > >

> > > # nc -l -p 5558 -c "tail -f /var/log/syslog"

> >

> > while :; do nc -l -p 5558 -c "tail -f /var/log/syslog"; done

> >

> Michael, thanks for your reply. But if you read my entire email, you'll

> see that the problem is netcat NOT exiting after disconnect. So, your

> script will not solve the problem, because the nc never exits.



You execute ``tail -f', which actually never returns. You may try the -w

delay option:



* * * *# while :; do nc -l -w 10 -p 5558 -c "tail -f /var/log/syslog"; done



Please avoid top posting.



Thanks,


Thanks for de tips about top posting. I"ll remember

Michael, -w option dont make nc returns if a conection is already stablished.

I've tried this: nc -l -p 5558 -c bash. And it works! I think the big probleam is that tail doesn't connect his input stream to the data provided by nc, so it doesn't get informed when the connection comes down. As you said, tail -f never returns. Bash returns, if required. But nc would kill the -c process when the connection doesn't exists anymore... it doesn't make sense keep it running.


I'm going to try another solutions for what I want... maybe xinetd...

Thanks Michael and you all.

Tod Detre 02-12-2009 09:40 PM

netcat in listen mode don't exit
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Paulo Brito wrote:
> Thanks for de tips about top posting. I"ll remember
>
> Michael, -w option dont make nc returns if a conection is already
> stablished.
>
> I've tried this: nc -l -p 5558 -c bash. And it works! I think the big
> probleam is that tail doesn't connect his input stream to the data
> provided by nc, so it doesn't get informed when the connection comes
> down. As you said, tail -f never returns. Bash returns, if required. But
> nc would kill the -c process when the connection doesn't exists
> anymore... it doesn't make sense keep it running.
>
> I'm going to try another solutions for what I want... maybe xinetd...
>
> Thanks Michael and you all.

If you want to just what's changed in the log file since the last time
you connected, look at the package logtail.

For example you might run this command:

while :; do nc -l -p 5558 -c "logtail /var/log/syslog"; done

This would have nc exit when it's done dumping /var/log/syslog and the
next time someone connects it should pick up where it left off.

Regards,
Tod Detre

- --
Pantek, Inc. - http://www.pantek.com/ - info@pantek.com
1-877-LINUX-FIX - Expert Open Source Technical Support
2008 Inductee to the prestigious Weatherhead 100
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmUpXkACgkQBp/fZcDt4Jv7+ACg0EWa2ZBuk0krUtCuPdplxiVr
ooEAn2QaMwhJ0MyIlXlI8B1CxWYnvfbW
=6dNT
-----END PGP SIGNATURE-----


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Paulo Brito 02-13-2009 09:26 AM

netcat in listen mode don't exit
 
If you want to just what's changed in the log file since the last time

you connected, look at the package logtail.



For example you might run this command:



while :; do nc -l -p 5558 -c "logtail /var/log/syslog"; done



This would have nc exit when it's done dumping /var/log/syslog and the

next time someone connects it should pick up where it left off.



Regards,

Tod Detre




Nice tip, Tod. But the idea was keeping a connection open, to get client informed by server that something happened.

Anyway, looking in netcat sources yesterday I found the reason of that behavior. It uses the "execl" system call to run -e program (the version I looked don't have -c anymore). Execl just substitute the current process with the new one, and execl never returns! So netcat sets up the network connections and pipes and go off the scenes. And so, netcat got no chances of killing the -e program. That's the reason why when -e program exits, the connection is gone. In my specific case, thare's another issue: tail -f dont care about stdin and never returns.


Maybe someday a new feature appers in netcat: run -e/-c program in a fork or something, so netcat can still have the control.

Eric Gerlach 02-13-2009 01:30 PM

netcat in listen mode don't exit
 
On Fri, Feb 13, 2009 at 07:26:50AM -0300, Paulo Brito wrote:
> >
> > If you want to just what's changed in the log file since the last time
> > you connected, look at the package logtail.
> >
> > For example you might run this command:
> >
> > while :; do nc -l -p 5558 -c "logtail /var/log/syslog"; done
> >
> > This would have nc exit when it's done dumping /var/log/syslog and the
> > next time someone connects it should pick up where it left off.
> >
> > Regards,
> > Tod Detre
> >
> >
> Nice tip, Tod. But the idea was keeping a connection open, to get client
> informed by server that something happened.
>
> Anyway, looking in netcat sources yesterday I found the reason of that
> behavior. It uses the "execl" system call to run -e program (the version I
> looked don't have -c anymore). Execl just substitute the current process
> with the new one, and execl never returns! So netcat sets up the network
> connections and pipes and go off the scenes. And so, netcat got no chances
> of killing the -e program. That's the reason why when -e program exits, the
> connection is gone. In my specific case, thare's another issue: tail -f dont
> care about stdin and never returns.
>
> Maybe someday a new feature appers in netcat: run -e/-c program in a fork or
> something, so netcat can still have the control.

If you're running this as a service, you might want to just set it up in inetd.
Assuming you're using inetd and not xinetd or others, your line might look
something like this:

5558 stream tcp nowait root /usr/bin/tail tail -F /var/log/syslog

I haven't tried that, nor have I tested that it does what you want. But it may
be worth a shot. I've never tried putting a command that wasn't designed for
inetd in its config before. If you try it I'm curious how it ends up working
for you.

Cheers,

--
Eric Gerlach, Network Administrator
Federation of Students
University of Waterloo
p: (519) 888-4567 x36329
e: egerlach@feds.uwaterloo.ca


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


All times are GMT. The time now is 09:28 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.