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 = nil) ⇒ Array

Enumerate CIDR addresses, single IPs and IP ranges.

Parameters:

  • ip (String)

    IP address

  • port (Fixnum) (defaults to: nil)

    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
# File 'lib/ssh_scan/target_parser.rb', line 11

def enumerateIPRange(ip,port=nil)
  if ip.fqdn?
    if port.nil?
      socket = ip
    else
      socket = ip.concat(":").concat(port.to_s)
    end
    return [socket]
  else
    if ip.include? "/"
      begin
        ip_net = NetAddr::IPv4Net.parse(ip)
      rescue
        raise ArgumentError, "Invalid target: #{ip}"
      end

      sock_array = []
      1.upto(ip_net.len - 2) do |i|
        sock_array << ip_net.nth(i).to_s
      end

      if !port.nil?
        sock_array.map! { |i| i.concat(":").concat(port.to_s) }
      end
      return sock_array
    else
      if port.nil?
        socket = ip
      else
        socket = ip.concat(":").concat(port.to_s)
      end
      return [socket]
    end
  end
end