Class: SSHScan::TargetParser

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

Overview

Enumeration methods for IP notations.

Instance Method Summary collapse

Instance Method Details

#enumerateIPRange(ip, port) ⇒ Array

Enumerate CIDR addresses, single IPs and IP ranges.

Parameters:

  • ip (String)

    IP address

  • port (Fixnum)

    port

Returns:

  • (Array)

    array of enumerated addresses



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ssh_scan/target_parser.rb', line 11

def enumerateIPRange(ip,port)
  if ip.fqdn?
    if port.nil?
      socket = ip
    else
      socket = ip.concat(":").concat(port.to_s)
    end
    return [socket]
  else
    if ip.include? "-"
      octets = ip.split('.')
      range = octets.pop.split('-')
      lower = NetAddr::CIDR.create(octets.join('.') + "." + range[0])
      upper = NetAddr::CIDR.create(octets.join('.') + "." + range[1])
      ip_array = NetAddr.range(lower, upper,:Inclusive => true)
      if !port.nil?
        ip_array.map! { |i| i.concat(":").concat(port.to_s) }
      end
      return ip_array
    elsif ip.include? "/"
      begin
        cidr = NetAddr::CIDR.create(ip)
      rescue
        raise ArgumentError, "Invalid target: #{ip}"
      end
      ip_array = cidr.enumerate
      ip_array.delete(cidr.network)
      ip_array.delete(cidr.last)
      if !port.nil?
        ip_array.map! { |i| i.concat(":").concat(port.to_s) }
      end
      return ip_array
    else
      if port.nil?
        socket = ip
      else
        socket = ip.concat(":").concat(port.to_s)
      end
      return [socket]
    end
  end
end