Instance Method Details

#is_port_open?(host, port) ⇒ Boolean

Checks if a port is open (listening) on a given host and port.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# File 'lib/vagrant/util/is_port_open.rb', line 16defis_port_open?(host,port)# We wrap this in a timeout because once in awhile the TCPSocket
# _will_ hang, but this signals that the port is closed.
Timeout.timeout(1)do# Attempt to make a connection
s=TCPSocket.new(host,port)# A connection was made! Properly clean up the socket, not caring
# at all if any exception is raised, because we already know the
# result.
s.closerescuenil# The port is open if we reached this point, since we were able
# to connect.
returntrueendrescueTimeout::Error,Errno::ECONNREFUSED,Errno::EHOSTUNREACH,Errno::ENETUNREACH# Any of the above exceptions signal that the port is closed.
returnfalseend