netcat - keep listening for connection in Debian

43,641

Solution 1

Debian's implementation of Netcat does have the -k option. However, it's not documented in the manual because it doesn't work in a reliable manner, for some unknown reason.

Luckily, there's ncat, which is yet another implementation of Netcat and is part of the nmap package. This one has a working -k. You can get it by installing nmap. ncat's options and usage are more or less similar to other implementations of Netcat, so your knowledge of other implementations should transfer very well to ncat.

Solution 2

I have the same problem if netcat gets a rst or fin packet, I think.

You could simply call netcat again as soon as it closes using a bash loop.

while true; do nc -lv <listeningport> ; done
Share:
43,641

Related videos on Youtube

Peta Sittek
Author by

Peta Sittek

Updated on September 18, 2022

Comments

  • Peta Sittek
    Peta Sittek over 1 year

    There's -k option in OS X (BSD) version of netcat to keep listening after current connection is completed. However in Debian (GNU?) version this option is missing.

    There's -q -1 option to listen forever after EOF appears on stdin but this doesn't do the trick and connections close anyway.

    Is there any way to force Debian's netcat to keep listening indefinitely?

  • Aalex Gabi
    Aalex Gabi over 6 years
    Thank you! Thanks to you I found best version of netcat ever.
  • Andrew Savinykh
    Andrew Savinykh almost 6 years
    openbsd-netcat appears to have the -k option as well
  • Robin Thoni
    Robin Thoni about 5 years
    Package is netcat-openbsd, not openbsd-netcat for those who might not click @AndrewSavinykh link
  • peterh
    peterh about 4 years
    Welcome on the SU! Please explain, what your code is doing and how does it work.
  • Jens Timmerman
    Jens Timmerman about 4 years
    This is different from ncat or opensbd-ncat -k option, which will actually allow multiple clients to connect at the same time; your solution will only allow one after the other
  • Vishrant
    Vishrant over 3 years
    ncat worked for me in macos
  • Tinmarino
    Tinmarino over 3 years
    @peterh-ReinstateMonica it is doing a loop that starts a new netcat command when the previous one has returned. While true for infinite loop.
  • bmaupin
    bmaupin over 2 years
    This doesn't work for me. After the message is sent, netcat just hangs until I press Enter. Maybe because I'm listening on UDP? Adding -w0 to the netcat command fixes it, e.g. while true; do nc -luv 5140 -w0; done
  • Abdull
    Abdull about 2 years
    ncat, UDP and -k don't work together: ncat -u -l -k 1900 results in Ncat: UDP mode does not support the -k or --keep-open options, except with --exec or --sh-exec. QUITTING.