Skip to content

Instantly share code, notes, and snippets.

@alvin2ye
Created June 22, 2010 01:32
Show Gist options
  • Select an option

  • Save alvin2ye/447802 to your computer and use it in GitHub Desktop.

Select an option

Save alvin2ye/447802 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