Sha256: e3e3b58175249003556f705b312897fc04fc14fcc577fb7e17465c565fa0521c

Contents?: true

Size: 910 Bytes

Versions: 8

Compression:

Stored size: 910 Bytes

Contents

require "socket"

module Vagrant
  module Util
    # 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.
    module IsPortOpen
      # Checks if a port is open (listening) on a given host and port.
      #
      # @param [String] host Hostname or IP address.
      # @param [Integer] port Port to check.
      # @return [Boolean] `true` if the port is open (listening), `false`
      #   otherwise.
      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

      extend IsPortOpen
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
vagrant-unbundled-2.3.6.0 lib/vagrant/util/is_port_open.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.3.3.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.3.2.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.2.19.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.2.18.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.2.16.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.2.14.0 lib/vagrant/util/is_port_open.rb