Class: Hooray::Seek

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

Overview

Main runner

Constant Summary collapse

NET_MASK =
24
TIMEOUT =
1
RANGE_REGEX =
/\.{2,3}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network = nil, *params) ⇒ Seek

Returns a new instance of Seek.



12
13
14
15
16
# File 'lib/hooray/seek.rb', line 12

def initialize(network = nil, *params)
  @scan = {}
  config_network network
  config_filters params
end

Instance Attribute Details

#networkObject

Returns the value of attribute network.



6
7
8
# File 'lib/hooray/seek.rb', line 6

def network
  @network
end

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/hooray/seek.rb', line 6

def opts
  @opts
end

#portsObject

Returns the value of attribute ports.



6
7
8
# File 'lib/hooray/seek.rb', line 6

def ports
  @ports
end

Instance Method Details

#config_filters(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hooray/seek.rb', line 22

def config_filters(params)
  return if params.empty?
  # Map services 'foo' -> 3000 udp
  params.map! { |o| Settings.service(o) || o }
  # Map Ranges 1..3 -> [1,2,3]
  params.map! do |o|
    next o unless o =~ RANGE_REGEX
    Range.new(*o.split(RANGE_REGEX)).to_a
  end
  numbers, words = params.flatten.partition { |s| s.to_s =~ /\d+/ }
  @ports, @protocol = numbers.map(&:to_i), words.join
end

#config_network(str) ⇒ Object



18
19
20
# File 'lib/hooray/seek.rb', line 18

def config_network(str)
  @network = str && !str.empty? ? IPAddr.new(str) : Hooray::Local.mask
end

#nodesObject Also known as: devices

Map results to @nodes << Node.new()



38
39
40
41
42
43
# File 'lib/hooray/seek.rb', line 38

def nodes
  return @nodes if @nodes
  @nodes = sweep.map do |k, v|
    Node.new(ip: k, mac: Hooray::Local.arp_table[k.to_s], ports: v)
  end # .reject { |n| n.mac.nil? } # remove those without mac
end

#ping_classObject

Decide how to ping



48
49
50
51
52
# File 'lib/hooray/seek.rb', line 48

def ping_class
  return Net::Ping::External unless ports
  return Net::Ping::TCP unless @protocol =~ /tcp|udp|http|wmi/
  Net::Ping.const_get(@protocol.upcase)
end

#scan_bot(ip) ⇒ Object

Creates a bot per port on IP



56
57
58
59
60
61
62
63
64
65
# File 'lib/hooray/seek.rb', line 56

def scan_bot(ip)
  (ports || [nil]).each do |port|
    Thread.new do
      if ping_class.new(ip.to_s, port, TIMEOUT).ping?
        @scan[ip] << port
        print '.'
      end
    end
  end
end

#sweepObject

fast -> -sn -PA



70
71
72
73
74
75
76
77
# File 'lib/hooray/seek.rb', line 70

def sweep
  network.to_range.each do |ip|
    @scan[ip] = []
    scan_bot(ip)
  end
  Thread.list.reject { |t| t == Thread.current }.each(&:join)
  @scan.reject! { |_k, v| v.empty? }
end

#to_sObject



79
80
81
# File 'lib/hooray/seek.rb', line 79

def to_s
  "Seek #{network} #{ports}"
end