Skip to content

Instantly share code, notes, and snippets.

@rainly
Forked from alvin2ye/is_port_open
Created March 17, 2011 10:27
Show Gist options
  • Select an option

  • Save rainly/874128 to your computer and use it in GitHub Desktop.

Select an option

Save rainly/874128 to your computer and use it in GitHub Desktop.
# http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open
require 'socket'
require 'timeout'
def is_port_open?(ip, port)
begin
Timeout::timeout(1) do
begin
s = TCPSocket.new(ip, port)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
return false
end
end
rescue Timeout::Error
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment