No worries jmc, I was looking for a simple portscanner code snippet in ruby for a project I was working on and this is about the first one that pops up on Google.
By the way, if you don't want to mess around with non blocking sockets but you do want a timeout on that TCP connect attempt you can add the following code:
require 'socket'
puts "Enter IP Address to Scan:"
ipaddress = gets
1.upto(1024) {|port|
begin
timeout(5) do
TCPSocket.open(ipaddress.chop, port)
end
puts "Response/Port Open: #{port}"
rescue Timeout::Error
# uncomment the following line to show closed ports (noisy!)
#puts "No Response / Port closed: #{port}"
rescue
# uncomment the following line to show closed ports (noisy!)
#puts "No Response / Port closed: #{port}"
end
}
Kind of an easy way in Ruby to put a timer on stuff.