nc (netcat) on Mac OS X 10.8.4 gets stuck

前端 未结 2 1475
暖寄归人
暖寄归人 2021-01-06 07:04

I encountered a little issue while using the nc utility on Mac OS X, a utility i often use as a quick and dirty solution to check if a port is open and what version the daem

相关标签:
2条回答
  • 2021-01-06 07:17

    I tried this on my Mac running 10.8.4 and after about 6 minutes it was looking like:

    andys-MacBook-Pro:EquipDB uw$ for i in {183..200}; do echo "hello" | nc -n -w 2 -v 10.120.113.$i 22; done
    nc: connect to 10.120.113.183 port 22 (tcp) failed: Operation timed out
    nc: connect to 10.120.113.184 port 22 (tcp) failed: Operation timed out
    nc: connect to 10.120.113.185 port 22 (tcp) failed: Operation timed out
    nc: connect to 10.120.113.185 port 22 (tcp) failed: Operation timed out
    

    So it is timing out on mine, but just takes quite a while.

    Hmmm... was thinking my test would be pointless because I will never actually connect to something since I'm not on your network. But I did see this:

    -w # => Timeout after # seconds

    Note that -w also sets the network inactivity timeout. This does not have any effect until standard input closes, but then if nothing further arrives from the network in the next seconds, netcat tries to read the net once more for good measure, and then closes and exits. There are a lot of network services now that accept a small amount of input and return a large amount of output, such as Gopher and Web servers, which is the main reason netcat was written to ``block'' on the network staying open rather than standard input. Handling the timeout this way gives uniform behavior with network servers that don't close by themselves until told to.

    This works for final net reads but not for connections.

    If I'm understanding this right it only times out once it connects, since mine never connects it just has a built in timeout that has nothing to do with -w? found here, helpful?

    0 讨论(0)
  • 2021-01-06 07:24

    I believe you are looking for the -G option. From the man pages:

    -G conntimeout TCP connection timeout in seconds.

    -w is used to set the timeout after connection. -G option is used to set the timeout before connection. This should give you what you want

    nc -n -G 2 -v xxx.xxx.xxx.xxx 22
    
    0 讨论(0)
提交回复
热议问题