Skip to content

Instantly share code, notes, and snippets.

@willryan
Created January 13, 2015 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willryan/9099c21199ad3adf2a45 to your computer and use it in GitHub Desktop.
Save willryan/9099c21199ad3adf2a45 to your computer and use it in GitHub Desktop.
Checking if a TCP port is in use
def is_port_open?(ip, port)
begin
Timeout::timeout(20) 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