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
Public Class Methods
Source
Source
# File lib/ronin/support/network/ip_range.rb, line 171 def self.each(string,&block) new(string).each(&block) end
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
Source
# File lib/ronin/support/network/ip_range.rb, line 197 def self.glob?(string) string =~ Glob::REGEX end
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.
Source
# 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
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.
Source
# File lib/ronin/support/network/ip_range.rb, line 119 def self.parse(string) new(string) end
Alias for {#initialize new}.
@param [String] string
The IP range to parse.
@return [IPRange]
The parsed IP range.
@see initialize
Public Instance Methods
Source
Source
# File lib/ronin/support/network/ip_range.rb, line 279 def ===(other) case other when IPRange @range === other.range else @range === other end end
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]
@since 1.1.0
Source
# File lib/ronin/support/network/ip_range.rb, line 336 def each(&block) @range.each(&block) end
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
Source
# File lib/ronin/support/network/ip_range.rb, line 350 def first @range.first end
The first IP
address in the IP
range.
@return [String]
@see CIDR#first
@see Glob#last
@since 1.1.0
Source
# File lib/ronin/support/network/ip_range.rb, line 248 def include?(ip) @range.include?(ip) end
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
Source
# File lib/ronin/support/network/ip_range.rb, line 399 def inspect "#<#{self.class}: #{@range.string}>" end
Inspects the IP
range.
@return [String]
Source
# File lib/ronin/support/network/ip_range.rb, line 221 def ipv4? @range.ipv4? end
Determines if the IP
range is IPv4.
@return [Boolean]
@see CIDR#ipv4? @see Glob#ipv4?
Source
# File lib/ronin/support/network/ip_range.rb, line 233 def ipv6? @range.ipv6? end
Determines if the IP
range is IPv6.
@return [Boolean]
@see CIDR#ipv6? @see Glob#ipv6?
Source
# File lib/ronin/support/network/ip_range.rb, line 364 def last @range.last end
The last IP
address in the IP
range.
@return [String]
@see CIDR#first
@see Glob#last
@since 1.1.0
Source
Source
# File lib/ronin/support/network/ip_range.rb, line 209 def string @range.string end
The IP
range string.
@return [String]
@see CIDR#string
@see Glob#string