Netcat: using nc -l port_number instead of nc -l -p port_number

后端 未结 2 795
北海茫月
北海茫月 2021-01-13 11:04

This question is following this one: Sockets working in openSUSE do not work in Debian?

When working with sockets on my Debian system, I have to use nc -l -p port_nu

相关标签:
2条回答
  • 2021-01-13 11:34

    I agree with duskwuff that it is better to just use the -p option everywhere, but to answer your question:

    The one thing you have to do is install a netcat that supports the syntax you want. I know the netcat-openbsd package supports it. I know the netcat-traditional package does not. There's also a netcat6 package, which also doesn't. You can then explicitly request the OpenBSD version of netcat like so:

    nc.openbsd -l 4242
    

    Optionally you may use the alternatives system to set this version of netcat to run when you issue the nc command:

    update-alternatives --set nc /bin/nc.openbsd
    

    This will be done automatically for you if this is the only netcat you've installed.

    Finally, you may, again optionally, remove the netcat you don't like (netcat-traditional or netcat6).

    0 讨论(0)
  • 2021-01-13 11:41

    Do not adjust your set. There are multiple implementations of netcat out there; not all of them behave the same.

    In particular, the "traditional" version of netcat, which is probably what you have installed on your Debian system, will end up doing something totally unexpected if you omit the -p ("port") flag: it will end up treating the last argument as a hostname, pass it to inet_aton(), which will convert it to a nonsensical IP address (e.g, 1234 will become 0.0.4.210), and will then proceed to ignore that IP address and listen on a socket with an automatically assigned (probably random) port number.

    This behavior is obviously silly, so some other implementations of netcat will assume you meant -p. The one you're using doesn't, though, so pass the -p option.

    0 讨论(0)
提交回复
热议问题