-
-
Save rainly/874128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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