Class: Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/portscanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScanner

Returns a new instance of Scanner.



5
6
7
8
9
# File 'lib/portscanner.rb', line 5

def initialize
  @opened = []
  @closed = []
  @log    = []
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



4
5
6
# File 'lib/portscanner.rb', line 4

def log
  @log
end

Instance Method Details

#multi(host, p_start, p_end) ⇒ Object



31
32
33
# 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) ⇒ Boolean

Returns:



11
12
13
14
15
16
17
18
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# 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

#statusObject



35
36
37
38
# 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