class Ronin::Support::Network::IPRange

Represents an IP range.

## Examples

Enumerating over a CIDR range:

IPRange.each('10.0.0.1/24') { |ip| puts ip }
# 10.0.0.0
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.254
# 10.0.0.255

Enumerating over a IP-glob range:

IPRange.each('10.0.1-3.*') { |ip| puts ip }
# 10.0.1.0
# 10.0.1.1
# ...
# 10.0.1.254
# 10.0.1.255
# ...
# 10.0.2.0
# 10.0.2.1
# ...
# 10.0.2.254
# 10.0.2.255
# ...
# 10.0.3.0
# 10.0.3.1
# ...
# 10.0.3.254
# 10.0.3.255

@api public

@since 1.0.0

Constants

REGEX

Regular expression to match CIDR ranges or IP-glob ranges.

@api private

@since 1.1.0

Attributes

range[R]

The parsed IP range.

@return [CIDR, Glob]

@api private

@since 1.1.0

Public Class Methods

cidr?(string) click to toggle source

Determines if the IP range is a CIDR range.

@param [String] string

The IP range string to inspect.

@return [Boolean]

Indicates that the IP range is a CIDR range.
# File lib/ronin/support/network/ip_range.rb, line 184
def self.cidr?(string)
  string =~ CIDR::REGEX
end
each(string,&block) click to toggle source

Enumerates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.

@param [String] string

The IP range string to parse and enumerate over.

@yield [ip]

The block which will be passed every IP address covered be the
netmask of the IPAddr object.

@yieldparam [String] ip

An IP address.

@return [Enumerator]

If no block is given an Enumerator will be returned.

@example Enumerating over a CIDR range:

IPRange.each('10.0.0.1/24') { |ip| puts ip }
# 10.0.0.0
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.254
# 10.0.0.255

@example Enumerating over a IP-glob range:

IPRange.each('10.0.1-3.*') { |ip| puts ip }
# 10.0.1.0
# 10.0.1.1
# ...
# 10.0.1.254
# 10.0.1.255
# ...
# 10.0.2.0
# 10.0.2.1
# ...
# 10.0.2.254
# 10.0.2.255
# ...
# 10.0.3.0
# 10.0.3.1
# ...
# 10.0.3.254
# 10.0.3.255

@see each

# File lib/ronin/support/network/ip_range.rb, line 171
def self.each(string,&block)
  new(string).each(&block)
end
glob?(string) click to toggle source

Determines if the IP range is a IP-glob range.

@param [String] string

The IP range string to inspect.

@return [Boolean]

Indicates that the IP range is a IP-glob range.
# File lib/ronin/support/network/ip_range.rb, line 197
def self.glob?(string)
  string =~ Glob::REGEX
end
new(string) click to toggle source

Initializes the IP range.

@param [String] string

The IP range string.

@example Initializing a CIDR IP range:

ip_range = IPRange.new('10.0.0.1/24')

@example Initializing an IP-glob range:

ip_range = IPRange.new('10.0.1-3.*')

@raise [ArgumentError]

The IP range was neither a CIDR range or a IP-glob range.
# File lib/ronin/support/network/ip_range.rb, line 99
def initialize(string)
  @range = case string
           when CIDR::REGEX then CIDR.new(string)
           when Glob::REGEX then Glob.new(string)
           else
             raise(ArgumentError,"invalid IP range: #{string.inspect}")
           end
end
parse(string) click to toggle source

Alias for {#initialize new}.

@param [String] string

The IP range to parse.

@return [IPRange]

The parsed IP range.

@see initialize

# File lib/ronin/support/network/ip_range.rb, line 119
def self.parse(string)
  new(string)
end

Public Instance Methods

==(other) click to toggle source

Compares the IP range to another IP range.

@param [Object] other

The other IP range.

@return [Boolean]

@since 1.1.0

# File lib/ronin/support/network/ip_range.rb, line 262
def ==(other)
  other.kind_of?(self.class) && @range == other.range
end
===(other) click to toggle source

Determines if the given IP range is a sub-set of the IP range.

@param [IPRange, Enumerable<String>] other

The other IP range.

@return [Boolean]

@see CIDR#=== @see Glob#===

@since 1.1.0

# File lib/ronin/support/network/ip_range.rb, line 279
def ===(other)
  case other
  when IPRange
    @range === other.range
  else
    @range === other
  end
end
each(&block) click to toggle source

Enumerates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.

@yield [ip]

The block which will be passed every IP address covered be the
netmask of the IPAddr object.

@yieldparam [String] ip

An IP address.

@return [Enumerator]

If no block is given an Enumerator will be returned.

@example Enumerating over a CIDR range:

ip_range = IPRange.new('10.0.0.1/24')
ip_range.each { |ip| puts ip }
# 10.0.0.0
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.254
# 10.0.0.255

@example Enumerating over a IP-glob range:

ip_range = IPRange.new('10.0.1-3.*')
ip_range.each { |ip| puts ip }
# 10.0.1.0
# 10.0.1.1
# ...
# 10.0.1.254
# 10.0.1.255
# ...
# 10.0.2.0
# 10.0.2.1
# ...
# 10.0.2.254
# 10.0.2.255
# ...
# 10.0.3.0
# 10.0.3.1
# ...
# 10.0.3.254
# 10.0.3.255

@see CIDR#each @see Glob#each

# File lib/ronin/support/network/ip_range.rb, line 336
def each(&block)
  @range.each(&block)
end
first() click to toggle source

The first IP address in the IP range.

@return [String]

@see CIDR#first @see Glob#last

@since 1.1.0

# File lib/ronin/support/network/ip_range.rb, line 350
def first
  @range.first
end
include?(ip) click to toggle source

Determines whether the IP address exists within the IP range.

@param [IP, IPAddr, String] ip

The IP address to check.

@return [Boolean]

Indicates whether the IP address exists within the IP range.

@since 1.1.0

# File lib/ronin/support/network/ip_range.rb, line 248
def include?(ip)
  @range.include?(ip)
end
inspect() click to toggle source

Inspects the IP range.

@return [String]

# File lib/ronin/support/network/ip_range.rb, line 399
def inspect
  "#<#{self.class}: #{@range.string}>"
end
ipv4?() click to toggle source

Determines if the IP range is IPv4.

@return [Boolean]

@see CIDR#ipv4? @see Glob#ipv4?

# File lib/ronin/support/network/ip_range.rb, line 221
def ipv4?
  @range.ipv4?
end
ipv6?() click to toggle source

Determines if the IP range is IPv6.

@return [Boolean]

@see CIDR#ipv6? @see Glob#ipv6?

# File lib/ronin/support/network/ip_range.rb, line 233
def ipv6?
  @range.ipv6?
end
last() click to toggle source

The last IP address in the IP range.

@return [String]

@see CIDR#first @see Glob#last

@since 1.1.0

# File lib/ronin/support/network/ip_range.rb, line 364
def last
  @range.last
end
size() click to toggle source

Calculates the size of the IP range.

@return [Integer]

@see CIDR#size @see Glob#size

@since 1.1.0

# File lib/ronin/support/network/ip_range.rb, line 378
def size
  @range.size
end
string() click to toggle source

The IP range string.

@return [String]

@see CIDR#string @see Glob#string

# File lib/ronin/support/network/ip_range.rb, line 209
def string
  @range.string
end
to_s() click to toggle source

Converts the IP range back to a String.

@return [String]

@see CIDR#to_s @see Glob#to_s

# File lib/ronin/support/network/ip_range.rb, line 390
def to_s
  @range.to_s
end