2

I can run this netcat command to listen to ports only when I'm listing a single port, for example:

sudo ncat -lp 80 -w 10

However when I attempt to enter multiple port numbers it says the port number is invalid:

<user>@<pcname>:~$ sudo ncat -lp 22,80,8080 -w 10
Ncat: Invalid source port number "22,80,8080". QUITTING.

When I googled it I found that you can enter multiple ports with commas in between them.

So how do I modify the command to check multiple ports at once? Or do I have to enter the command multiple times?

1 Answer 1

3

ncat doesn't support listening (or connecting) to multiple ports. Where did you read that? Never trust Copilot. Use man ncat.

I don't exclude one / some of the many Netcat implementations would allow this by default, however in this case it's fairly easy to script opening multiple ports (I chose saner unprivileged ports):

for port in 10001 10002 10003; do ncat -l $port & done

Or for short, if you're opening a range:

for port in {10001..10003}; do ncat -l $port & done
user@[REDACTED] ~ % for port in {10001..10003}; do ncat -l $port & done
[2] 55126
[3] 55127
[4] 55128

You can then manage the processes using job control (fg, Ctrl+Z, Ctrl+C etc).

user@[REDACTED] ~ % ss -lp | grep ncat
tcp   LISTEN 0      10                                                0.0.0.0:10001                     0.0.0.0:*    users:(("ncat",pid=55261,fd=4))                                      
tcp   LISTEN 0      10                                                0.0.0.0:10003                     0.0.0.0:*    users:(("ncat",pid=55263,fd=4))                                      
tcp   LISTEN 0      10                                                0.0.0.0:10002                     0.0.0.0:*    users:(("ncat",pid=55262,fd=4))                                      
tcp   LISTEN 0      10                                                   [::]:10001                        [::]:*    users:(("ncat",pid=55261,fd=3))                                      
tcp   LISTEN 0      10                                                   [::]:10003                        [::]:*    users:(("ncat",pid=55263,fd=3))                                      
tcp   LISTEN 0      10                                                   [::]:10002                        [::]:*    users:(("ncat",pid=55262,fd=3))
6
  • 1
    I found that from bing copilot but i couldn't find it again (the multiple port thing). I have implemented what you shared and there is one remaining question. After a while it times out and then the ports are closed. How can i add extra code to prevent this such as a while true loop or possibly just manually adding an amount of time to the ncat command. I assume if i wanted to do that i would just add the -w option with a set amount of time
    – Samir Fink
    Apr 17 at 1:00
  • Thanks! setting -w flag for a lenghty amout of time seems to mess with netcat. If i do 240 minutes it says "-w timeout is seconds, so your tiem of '14400' is 240.0 minutes. Use 14400ms for 14400 miliseconds, QUITTING." Which makes me wonder if i can add --force or something like that
    – Samir Fink
    Apr 17 at 1:20
  • 2
    @SamirFink I'll rewrite my comment without the useless -w mention; you shouldn't trust Bing copilot too much :) and you really should start looking at man pages, man ncat would have teached you both that you can't listen / connect to multiple ports and that the -w flag is ignored; on the multiple ports thing, I wanna mention that there are lots of Netcat implementations, and I don't exclude some of them would allow to listen / connect to multiple ports at once.
    – kos
    Apr 17 at 1:21
  • 1
    It seems nc.openbsd doesn't support multiple ports, but it seems nc.traditional does: nc [-options] hostname port[s] [ports] ... and -p port local port number (port numbers can be individual or ranges: lo-hi [inclusive])
    – muru
    Apr 17 at 3:06
  • 1
    @muru Hmmm I tested using both /bin/nc.traditional -lp 10001-10003 and /bin/nc.traditional -lp 10001 10002 10003; they both opened port 10001, but not the others (on IPv4 addresses only, but that was kinda expected). Maybe that's for connecting to multiple ports? scratches head by looking at the synopsis it seems like that's the case, port[s] (plural) is mentioned only when describing the invocation case without -l
    – kos
    Apr 17 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .