Sha256: 851f6d312a44141d94a6ca0944a017b9199070d53e2e31ebc5ab72b3edf2303e

Contents?: true

Size: 1.41 KB

Versions: 20

Compression:

Stored size: 1.41 KB

Contents

require "socket"
require "timeout"

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)
        # 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.close rescue nil

          # The port is open if we reached this point, since we were able
          # to connect.
          return true
        end
      rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, \
             Errno::ENETUNREACH, Errno::EACCES, Errno::ENOTCONN
        # Any of the above exceptions signal that the port is closed.
        return false
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
vagrant-unbundled-2.2.9.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.2.8.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.2.7.0 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-2.0.0.1 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.9.8.1 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.9.7.1 lib/vagrant/util/is_port_open.rb
vagrant-aws-mkubenka-0.7.2.pre.14 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/is_port_open.rb
vagrant-aws-mkubenka-0.7.2.pre.11 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/is_port_open.rb
vagrant-aws-mkubenka-0.7.2.pre.10 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/is_port_open.rb
vagrant-aws-mkubenka-0.7.2.pre.9 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.9.5.1 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.9.1.1 lib/vagrant/util/is_port_open.rb
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/is_port_open.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/is_port_open.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/is_port_open.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.8.5.2 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.8.5.1 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.8.4.2 lib/vagrant/util/is_port_open.rb
vagrant-unbundled-1.8.4.1 lib/vagrant/util/is_port_open.rb