Module: Vagrant::Util::IsPortOpen

Extended by:
IsPortOpen
Included in:
Action::Builtin::HandleForwardedPortCollisions, IsPortOpen
Defined in:
lib/vagrant/util/is_port_open.rb

Overview

Contains the method #is_port_open? to check if a port is open (listening) or closed (not in use). This method isn't completely fool-proof, but it works enough of the time to be useful.

Instance Method Summary collapse

Instance Method Details

#is_port_open?(host, port) ⇒ Boolean

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

Parameters:

  • host (String)

    Hostname or IP address.

  • port (Integer)

    Port to check.

Returns:

  • (Boolean)

    true if the port is open (listening), false otherwise.



18
19
20
21
22
23
24
25
26
# File 'lib/vagrant/util/is_port_open.rb', line 18

def is_port_open?(host, port)
  begin
    Socket.tcp(host, port, connect_timeout: 0.1).close
    true
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, \
      Errno::ENETUNREACH, Errno::EACCES, Errno::ENOTCONN, Errno::EALREADY
    false
  end
end