Skip to content

Instantly share code, notes, and snippets.

@turboladen
Created August 1, 2013 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turboladen/6133524 to your computer and use it in GitHub Desktop.
Save turboladen/6133524 to your computer and use it in GitHub Desktop.
Ruby port scanner
# Thanks to http://rubysource.com/build-a-port-scanner-in-ruby
require 'celluloid'
require 'socket'
class ScanPort
include Celluloid
def initialize(start_port, end_port, host)
@start_port = start_port
@end_port = end_port
@host = host
end
def run
until @start_port == @end_port do
scan @start_port
@start_port += 1
end
end
def scan(port)
begin
sock = TCPSocket.new(@host, port)
puts "#{port} open." if sock
rescue => ex
p ex unless ex.is_a?(Errno::ECONNREFUSED)
ensure
sock.close if sock
end
end
end
def main
host = ARGV[0]
start_port = ARGV[1].to_i
end_port = ARGV[2].to_i
segment_size = 100
until start_port >= end_port do
sp = ScanPort.new start_port, start_port + segment_size, host
sp.async.run
start_port += segment_size
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment