Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created August 21, 2019 11:02
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 mamantoha/7e4b63c21c5721612c878f6abc4c189d to your computer and use it in GitHub Desktop.
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.
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