I need a quick way to find out if a given port is open with Ruby. I currently am fiddling around with this:
require \'socket\' def is_port_open?(ip, port)
More Ruby idiomatic syntax:
require 'socket' require 'timeout' def port_open?(ip, port, seconds=1) Timeout::timeout(seconds) do begin TCPSocket.new(ip, port).close true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH false end end rescue Timeout::Error false end