-   Linux - Software (
-   -   netcat question (

overbored 10-30-2004 05:14 AM

netcat question
hi all, i'd like to use netcat in a script to send some text via sockets, but i don't know how to have it close the connection once it has finished sending all the text. usually when i use netcat i just hit ctrl-c, but i won't be there to do that while the script is running.

i was told about a -q option on some irc channels. i downloaded and installed @stake's nc 1.10 via yum (on a fedora box) but that didn't have -q. i then built gnu netcat 0.7.1 from their site and that didn't have -q either. however on my debian and gentoo boxes i have -q (got pkgs via apt and emerge, respectively)...but the system i need to run it on is the fedora box. (i also tried downloading & building the tbz from @stake's/securityfocus' site but that failed miserably.)

i've also looked into netpipes but that seems to be entirely different from what i want (the message sender is actually the server).

thanks in advance for any help.

ToniT 10-30-2004 06:33 AM


nc -l -p 7421

echo 'foo' | nc -q0 othermachine 7421

overbored 10-30-2004 12:36 PM

the server is NOT netcat. and i already said there's no -q.

cetialphav 10-30-2004 01:12 PM

I don't understand the problem you are having. netcat will close the connection down automatically when it detects EOF on its stdin. So when there is nothing else to send, it shuts down automatically. When using this interactively, you can cause this by sending Ctrl-D.

overbored 10-30-2004 01:27 PM

neither GNU netcat nor @stake netcat closes the connection on eof on any box i've tried it on. and in all the time i've used netcat i've never ever seen that.

ToniT 10-30-2004 01:43 PM

Oh, sorry I wasn't reading very carefully..

If your netcat doesn't have -q -option, go a head and build a better one.
eg. from the sources of debian netcat.

ToniT 10-30-2004 01:45 PM

Oh... you already did that, too.

Why the building from debian sources failed?

overbored 10-30-2004 02:03 PM

as i mentioned... :) i need to run it on is a fedora box.

ToniT 10-30-2004 02:22 PM

Yes you did. (this time I readed the whole message (hopefully)).

What happened when you tried to build debian netcat either
-in debian, but linking it statically
-directly in fedora

Quick howto on building from debian sources:

apt-get install build-essential fakeroot
apt-get source foo
apt-get build-dep foo
cd foo_123.4-5
dpkg-buildpackage -rfakeroot

Quick howto on doing the same on non-debian box:
1. Surf into
2. Download the tar.gz and .diff.gz (maybe the dsc file too, for own reading).
3. Check the list of build-dependencies (none in this case).
4. Unpack and apply the debian patchset

tar xvfz foo_123.4.orig.tar.gz
zcat foo_123.4-5.diff.gz | patch -p0

5. If the package has patches split in parts (using dpatch), you have to apply them manually too.

cd foo_123.4.orig
chmod +x debian/patches/*
for i in `cat debian/patches/00list`; do debian/patches/$i.dpatch -patch ; done
touch patch-stamp

6. Compile the bastard.

chmod +x debian/rules
debian/rules build

cetialphav 10-30-2004 02:31 PM

There must be a disconnect here. I don't know why you think the connection is not closed. I'm running the netcat that comes with Fedora Core 2.


[cetialphav@perl cetialphav]$ echo "hello" | /usr/bin/nc localhost 23
? #'[cetialphav@perl cetialphav]$

Netcat opens a TCP connection to port 23, sends "hello", sees the EOF on stdin and closes the connection and exits. Is this not what you are asking for?

ToniT 10-30-2004 02:35 PM

Also building in debian sources can also be done in one command:

apt-src install -b foo
And to get the thing build static binaries,
patch the debian/rules before running dpkg-buildpackage.

Here's the patch:

diff -rub netcat-1.10/debian/rules new/netcat-1.10/debian/rules
--- netcat-1.10/debian/rules    2004-10-30 22:27:35.000000000 +0300
+++ new/netcat-1.10/debian/rules        2004-10-30 22:32:19.000000000 +0300
@@ -22,7 +22,7 @@
 build-stamp: patch-stamp
        $(MAKE) linux \
-          CFLAGS='$(DEB_CFLAGS)' STATIC='' \
+          CFLAGS='$(DEB_CFLAGS)' STATIC='-static' \
        touch build-stamp

overbored 10-30-2004 02:35 PM

cetialphav, you're wrong because the connection is closed *from the server side.* just because your netcat process ended doesn't mean that the client was the one who closed the connection. if you don't believe me just listen with netcat.

overbored 10-30-2004 02:46 PM

tonit, i will try that out once i can access the box again.

cetialphav 10-30-2004 04:26 PM

You mean listen with tcpdump (or ethereal). I did. It is not the server that closes the connection. The TCP connection is closed by the client (nc) and not the server. Are you using the -w option to netcat?

All times are GMT -5. The time now is 12:29 PM.