class Scanner

Attributes

log[R]

Public Class Methods

new() click to toggle source
# File lib/portscanner.rb, line 5
def initialize
  @opened = []
  @closed = []
  @log    = []
end

Public Instance Methods

multi(host, p_start, p_end) click to toggle source
# File lib/portscanner.rb, line 31
def multi(host, p_start, p_end)
  (p_start..p_end).each { |port| Thread.new { port_open?(host, port) } } 
end
port_open?(host, port) click to toggle source
# File lib/portscanner.rb, line 11
def port_open?(host, port)
  Socket.tcp(host, port, connect_timeout: 1) do |socket|
    @opened << port
  end
  rescue => error
    @log    << error.message
    @closed << port
end
run(host, p_start, p_end) click to toggle source
# File lib/portscanner.rb, line 20
def run(host, p_start, p_end)
  start  = p_start.to_i
  finish = p_end.to_i
  if p_end
    multi(host, start, finish)
  else
    port_open?(host, start)
  end
  status
end
status() click to toggle source
# File lib/portscanner.rb, line 35
def status
  puts "Opened: #{@opened.join(', ')}" if @opened.length > 0
  puts "Closed: #{@closed.join(', ')}" if @closed.length > 0
end