23

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?

30

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.

| improve this answer | |
  • 1
    Thank you! Thanks to you I found best version of netcat ever. – Aalex Gabi Oct 20 '17 at 20:04
  • 1
    openbsd-netcat appears to have the -k option as well – Andrew Savinykh Jun 19 '18 at 5:01
  • Package is netcat-openbsd, not openbsd-netcat for those who might not click @AndrewSavinykh link – Robin Thoni Apr 8 '19 at 22:22
1

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
| improve this answer | |
  • 2
    Welcome on the SU! Please explain, what your code is doing and how does it work. – peterh - Reinstate Monica Mar 5 at 21:33
  • 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 – Jens Timmerman Apr 9 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.