Created
August 21, 2019 11:02
-
-
Save mamantoha/7e4b63c21c5721612c878f6abc4c189d to your computer and use it in GitHub Desktop.
A quick way to find out if a given port is open with Crystal.
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
require "socket" | |
def is_port_open?(ip : String, port : Int32, timeout = 5) | |
s = TCPSocket.new(ip, port, dns_timeout: timeout, connect_timeout: timeout) | |
s.close | |
true | |
rescue IO::Timeout | |
false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment