Skip to content

Instantly share code, notes, and snippets.

@turboladen
Created August 1, 2013 17:36
Show Gist options
  • Select an option

  • Save turboladen/6133524 to your computer and use it in GitHub Desktop.

Select an option

Save turboladen/6133524 to your computer and use it in GitHub Desktop.

Revisions

  1. turboladen created this gist Aug 1, 2013.
    47 changes: 47 additions & 0 deletions porty.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # 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