Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active January 31, 2017 12:09
Show Gist options
  • Save snipsnipsnip/14ca07086dd2027ecc8f45cd218970e3 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/14ca07086dd2027ecc8f45cd218970e3 to your computer and use it in GitHub Desktop.
pingport.rb
#!/usr/bin/env ruby
# coding: utf-8
require 'socket'
ARGV.size == 2 or abort "usage: #$0 hostname port"
host, port = ARGV
sec = 1
addr = Addrinfo.tcp(host, port)
puts "trying #{addr.inspect_sockaddr}"
s = Socket.new(addr.afamily, addr.socktype)
begin
s.connect_nonblock addr
rescue IO::WaitWritable
IO.select(nil, [s], nil, sec) or abort "TIMEOUT"
end
puts "OPEN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment