Sha256: 16b7d6100b88eb780792490abc9405c874a7b301332cbca4816fbb1caeb8a4ee

Contents?: true

Size: 1.54 KB

Versions: 21

Compression:

Stored size: 1.54 KB

Contents

require 'netaddr'
require 'string_ext'

module SSHScan
  # Enumeration methods for IP notations.
  class TargetParser
    # Enumerate CIDR addresses, single IPs and IP ranges.
    # @param ip [String] IP address
    # @param port [Fixnum] port
    # @return [Array] array of enumerated addresses
    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
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ssh_scan-0.0.43 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.42 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.41 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.40 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.39 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.38 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.38.pre lib/ssh_scan/target_parser.rb
ssh_scan-0.0.37 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.36 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.35 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.34 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.33 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.32 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.31 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.30 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.29 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.28 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.27 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.26 lib/ssh_scan/target_parser.rb
ssh_scan-0.0.25 lib/ssh_scan/target_parser.rb