How to have netcat server close after n seconds

7,844

Solution 1

Some netcat implementations have a timeout option. For the one on my system, it is -w. Thus

$ nc -l -p 2000 -w 5

will listen on port 2000 and exit in 5 seconds unless a connection is attempted on that port.

Solution 2

Using bash timeout:

$ timeout 3s nc -l -p 2000

Solution 3

Because of the documentation :The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag

I tried nc ncat socat, no one can set timeout for server mode.

As far as I know, only busybox nc can follow -w option in server mode.

So you have to download busybox which compiled with CONFIG_NC_SERVER=y option, or compile it by your self.

So that you can

$busybox nc -w 10 -l -p 9999

But my system's busybox didn't compile with CONFIG_NC_SERVER=y option, and I don't want compile it. So I use this solution:

portnum=9999
(sleep 10 ;echo "T" | nc -w 1 127.0.0.1 $portnum) | nc -N -l -p $portnum 

After 10 seconds, send "T" to localhost:9999

The full code is

#!/bin/sh
portnum=9999
testmsg="Hello_World"
if [ "$( (sleep 10 ;echo "T" | nc -w 1 127.0.0.1 $portnum) | nc -N -l -p $portnum )" = $testmsg ]; then
  echo "Test pass"
else
  echo "Test not pass"
fi

If server received Hello_World in 10 seconds, it will print Test pass. Otherwise print Test not pass

You can try open another console, and type

echo "Hello_World" | nc -w 1 127.0.0.1 9999

to test it.

Solution 4

As stated by peterph the timeout (-w) while listening (-l) for connections works (only) with traditional nc implementations.

On Ubuntu such an implementation can be installed with apt install netcat-traditional and called with nc.traditional.

Share:
7,844

Related videos on Youtube

SpecialBomb
Author by

SpecialBomb

I am a freelancing coder that is not very advanced with what he has learned. I am a Linux user that moved from Windows (I hold Windows and Linux/Unix in high regards, I have no favorite, just Linux seems a lot more useful to me for what I do). I actively preform mathematics in research and recreation. I also play PC games, my favorite game being Quake.I enjoy collecting fine writing and drawing utensils, like the Rotring 600 and the Staedtler 925-25/35. I usually have no idea what I'm doing half the time.

Updated on September 18, 2022

Comments

  • SpecialBomb
    SpecialBomb almost 2 years

    I need to use nc in a weird way, where I want the server to first send a file, and then receive a file, and this repeats. I would like to have every instance of the server server close after some seconds. How can I do this in a script? I already have a client that can interact with the server.

  • mikeserv
    mikeserv over 8 years
    most nc's suck these days, though, because they are not kept. ncat - by the nmap people - is the exception, i think. and, of course, there is socat if you have the determination to learn a new programming language.
  • Daniel Trugman
    Daniel Trugman over 5 years
    This is incorrect. According to man nc(1): "The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag"
  • mosvy
    mosvy over 4 years
    That will kill the nc after 3s even if it had accepted a connection.
  • mosvy
    mosvy over 4 years
    ... which may actually be what the OP was asking, but it's not what the other answers do.
  • chenchuk
    chenchuk over 4 years
    actually I didn't try to accept connection... it was perfect for my use case (I need the port to accept connection just to make remote script happy).
  • mosvy
    mosvy over 4 years
    You should clearly explain what it's doing. The question is unclear and people come here with different ideas -- in this case the other two answers do something completely different than yours: they will wait up to N seconds for a connection, and if a connection happens within the N seconds, they will accept it and will continue servicing it even after the N seconds have passed.
  • chenchuk
    chenchuk over 4 years
    in my use case, I got remote ansible script which check 9092 port (kafka) and fails because kafka fails. since I don't care about this, I need a mechanism to make the port open. using nc in a loop did the job for me. ansinle tries to reach the port for 5 min, I made it to make ansible happy and continue execution. (after this I fix Kafka which is not relevant here)