Class: Ronin::Support::Network::IPRange

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/network/ip_range.rb,
lib/ronin/support/network/ip_range/cidr.rb,
lib/ronin/support/network/ip_range/glob.rb,
lib/ronin/support/network/ip_range/range.rb

Overview

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

Since:

  • 1.0.0

Defined Under Namespace

Classes: CIDR, Glob, Range

Constant Summary collapse

REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

Since:

  • 1.1.0

/#{CIDR::REGEX}|#{Glob::REGEX}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ IPRange

Initializes the IP range.

Examples:

Initializing a CIDR IP range:

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

Initializing an IP-glob range:

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

Parameters:

  • string (String)

    The IP range string.

Raises:

  • (ArgumentError)

    The IP range was neither a CIDR range or a IP-glob range.

Since:

  • 1.0.0



99
100
101
102
103
104
105
106
# 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

Instance Attribute Details

#rangeCIDR, Glob (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed IP range.

Returns:

Since:

  • 1.1.0



82
83
84
# File 'lib/ronin/support/network/ip_range.rb', line 82

def range
  @range
end

Class Method Details

.cidr?(string) ⇒ Boolean

Determines if the IP range is a CIDR range.

Parameters:

  • string (String)

    The IP range string to inspect.

Returns:

  • (Boolean)

    Indicates that the IP range is a CIDR range.

Since:

  • 1.0.0



184
185
186
# File 'lib/ronin/support/network/ip_range.rb', line 184

def self.cidr?(string)
  string =~ CIDR::REGEX
end

.each(string) {|ip| ... } ⇒ Enumerator

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

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

Parameters:

  • string (String)

    The IP range string to parse and enumerate over.

Yields:

  • (ip)

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

Yield Parameters:

  • ip (String)

    An IP address.

Returns:

  • (Enumerator)

    If no block is given an Enumerator will be returned.

See Also:

Since:

  • 1.0.0



171
172
173
# File 'lib/ronin/support/network/ip_range.rb', line 171

def self.each(string,&block)
  new(string).each(&block)
end

.glob?(string) ⇒ Boolean

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

Parameters:

  • string (String)

    The IP range string to inspect.

Returns:

  • (Boolean)

    Indicates that the IP range is a IP-glob range.

Since:

  • 1.0.0



197
198
199
# File 'lib/ronin/support/network/ip_range.rb', line 197

def self.glob?(string)
  string =~ Glob::REGEX
end

.parse(string) ⇒ IPRange

Alias for new.

Parameters:

  • string (String)

    The IP range to parse.

Returns:

  • (IPRange)

    The parsed IP range.

See Also:

Since:

  • 1.0.0



119
120
121
# File 'lib/ronin/support/network/ip_range.rb', line 119

def self.parse(string)
  new(string)
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the IP range to another IP range.

Parameters:

  • other (Object)

    The other IP range.

Returns:

  • (Boolean)

Since:

  • 1.1.0



262
263
264
# File 'lib/ronin/support/network/ip_range.rb', line 262

def ==(other)
  other.kind_of?(self.class) && @range == other.range
end

#===(other) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)

See Also:

Since:

  • 1.1.0



279
280
281
282
283
284
285
286
# 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 {|ip| ... } ⇒ Enumerator

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

Examples:

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

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

Yields:

  • (ip)

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

Yield Parameters:

  • ip (String)

    An IP address.

Returns:

  • (Enumerator)

    If no block is given an Enumerator will be returned.

See Also:

Since:

  • 1.0.0



336
337
338
# File 'lib/ronin/support/network/ip_range.rb', line 336

def each(&block)
  @range.each(&block)
end

#firstString

The first IP address in the IP range.



350
351
352
# File 'lib/ronin/support/network/ip_range.rb', line 350

def first
  @range.first
end

#include?(ip) ⇒ Boolean

Determines whether the IP address exists within the IP range.

Parameters:

Returns:

  • (Boolean)

    Indicates whether the IP address exists within the IP range.

Since:

  • 1.1.0



248
249
250
# File 'lib/ronin/support/network/ip_range.rb', line 248

def include?(ip)
  @range.include?(ip)
end

#inspectString

Inspects the IP range.

Returns:

Since:

  • 1.0.0



399
400
401
# File 'lib/ronin/support/network/ip_range.rb', line 399

def inspect
  "#<#{self.class}: #{@range.string}>"
end

#ipv4?Boolean

Determines if the IP range is IPv4.

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0



221
222
223
# File 'lib/ronin/support/network/ip_range.rb', line 221

def ipv4?
  @range.ipv4?
end

#ipv6?Boolean

Determines if the IP range is IPv6.

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0



233
234
235
# File 'lib/ronin/support/network/ip_range.rb', line 233

def ipv6?
  @range.ipv6?
end

#lastString

The last IP address in the IP range.



364
365
366
# File 'lib/ronin/support/network/ip_range.rb', line 364

def last
  @range.last
end

#sizeInteger

Calculates the size of the IP range.



378
379
380
# File 'lib/ronin/support/network/ip_range.rb', line 378

def size
  @range.size
end

#stringString

The IP range string.



209
210
211
# File 'lib/ronin/support/network/ip_range.rb', line 209

def string
  @range.string
end

#to_sString

Converts the IP range back to a String.



390
391
392
# File 'lib/ronin/support/network/ip_range.rb', line 390

def to_s
  @range.to_s
end