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

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.

Since:

  • 1.0.0



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

def initialize(string)
  @range = if self.class.glob?(string) then Glob.new(string)
           else                             CIDR.new(string)
           end
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



162
163
164
# File 'lib/ronin/support/network/ip_range.rb', line 162

def self.cidr?(string)
  !glob?(string)
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



149
150
151
# File 'lib/ronin/support/network/ip_range.rb', line 149

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



175
176
177
# File 'lib/ronin/support/network/ip_range.rb', line 175

def self.glob?(string)
  string.include?('*') || string.include?(',') || string.include?('-')
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



97
98
99
# File 'lib/ronin/support/network/ip_range.rb', line 97

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

Instance Method Details

#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



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

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

#inspectString

Inspects the IP range.

Returns:

Since:

  • 1.0.0



284
285
286
# File 'lib/ronin/support/network/ip_range.rb', line 284

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

#ipv4?Boolean

Determines if the IP range is IPv4.

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0



199
200
201
# File 'lib/ronin/support/network/ip_range.rb', line 199

def ipv4?
  @range.ipv4?
end

#ipv6?Boolean

Determines if the IP range is IPv6.

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0



211
212
213
# File 'lib/ronin/support/network/ip_range.rb', line 211

def ipv6?
  @range.ipv6?
end

#stringString

The IP range string.



187
188
189
# File 'lib/ronin/support/network/ip_range.rb', line 187

def string
  @range.string
end

#to_sString

Converts the IP range back to a String.



275
276
277
# File 'lib/ronin/support/network/ip_range.rb', line 275

def to_s
  @range.to_s
end