Sha256: da6465aee1886515cc19bb4f1e6e2df7578f139039ff962144f2b991bd0a36f4

Contents?: true

Size: 1.36 KB

Versions: 19

Compression:

Stored size: 1.36 KB

Contents

require "socket"
require "timeout"

module Vagrant
  module Util
    # Contains the method {#is_ruby_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
        # Any of the above exceptions signal that the port is closed.
        return false
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 6 rubygems

Version Path
tamtam-vagrant-reload-1.1.3 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/is_port_open.rb
tamtam-vagrant-reload-1.1.2 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/is_port_open.rb
tamtam-vagrant-reload-1.1.1 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/is_port_open.rb
tamtam-vagrant-reload-1.1 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/is_port_open.rb
tnargav-1.3.6 lib/vagrant/util/is_port_open.rb
tnargav-1.3.3 lib/vagrant/util/is_port_open.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/lib/vagrant/util/is_port_open.rb
tnargav-1.2.3 lib/vagrant/util/is_port_open.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/lib/vagrant/util/is_port_open.rb
vagrant-shell-0.2.6 vendor/bundle/gems/tnargav-1.2.2/lib/vagrant/util/is_port_open.rb
vagrant-shell-0.2.5 vendor/bundle/gems/tnargav-1.2.2/lib/vagrant/util/is_port_open.rb
tnargav-1.2.2 lib/vagrant/util/is_port_open.rb
vagrantup-1.1.3 lib/vagrant/util/is_port_open.rb
vagrantup-1.1.2 lib/vagrant/util/is_port_open.rb
vagrantup-1.1.1 lib/vagrant/util/is_port_open.rb
vagrantup-1.1.0 lib/vagrant/util/is_port_open.rb
vagrantup-1.1.4 lib/vagrant/util/is_port_open.rb
vagrant-actionio-0.0.9 vendor/bundle/bundler/gems/vagrant-c74251a1d9c0/lib/vagrant/util/is_port_open.rb
vagrant-lxc-0.0.1 vendor/vagrant/lib/vagrant/util/is_port_open.rb