Created
August 1, 2013 17:36
-
-
Save turboladen/6133522 to your computer and use it in GitHub Desktop.
Ruby port scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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