Ruby - See if a port is open

后端 未结 7 2013
灰色年华
灰色年华 2020-12-04 14:05

I need a quick way to find out if a given port is open with Ruby. I currently am fiddling around with this:

require \'socket\'

def is_port_open?(ip, port)
          


        
相关标签:
7条回答
  • 2020-12-04 15:06

    Just for completeness, the Bash would be something like this:

    $ netcat $HOST $PORT -w 1 -q 0 </dev/null && do_something
    

    -w 1 specifies a timeout of 1 second, and -q 0 says that, when connected, close the connection as soon as stdin gives EOF (which /dev/null will do straight away).

    Bash also has its own built-in TCP/UDP services, but they are a compile-time option and I don't have a Bash compiled with them :P

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