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.


15
16
17
18
19
20
21
22
23
# File 'lib/vagrant/util/is_port_open.rb', line 15

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