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.1
# 10.0.1.2
# ...
# 10.0.1.253
# 10.0.1.254
# ...
# 10.0.2.1
# 10.0.2.2
# ...
# 10.0.2.253
# 10.0.2.254
# ...
# 10.0.3.1
# 10.0.3.2
# ...
# 10.0.3.253
# 10.0.3.254

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.1
# 10.0.1.2
# ...
# 10.0.1.253
# 10.0.1.254
# ...
# 10.0.2.1
# 10.0.2.2
# ...
# 10.0.2.253
# 10.0.2.254
# ...
# 10.0.3.1
# 10.0.3.2
# ...
# 10.0.3.253
# 10.0.3.254

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.1
# 10.0.1.2
# ...
# 10.0.1.253
# 10.0.1.254
# ...
# 10.0.2.1
# 10.0.2.2
# ...
# 10.0.2.253
# 10.0.2.254
# ...
# 10.0.3.1
# 10.0.3.2
# ...
# 10.0.3.253
# 10.0.3.254

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



254
255
256
# File 'lib/ronin/support/network/ip_range.rb', line 254

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

#inspectString

Inspects the IP range.

Returns:

Since:

  • 1.0.0



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

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

#ipv4?Boolean

Determines if the IP range is IPv4.

Returns:

  • (Boolean)

Since:

  • 1.0.0



193
194
195
# File 'lib/ronin/support/network/ip_range.rb', line 193

def ipv4?
  @range.ipv4?
end

#ipv6?Boolean

Determines if the IP range is IPv6.

Returns:

  • (Boolean)

Since:

  • 1.0.0



202
203
204
# File 'lib/ronin/support/network/ip_range.rb', line 202

def ipv6?
  @range.ipv6?
end

#stringString

The IP range string.

Returns:

Since:

  • 1.0.0



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

def string
  @range.string
end

#to_sString

Converts the IP range back to a String.



266
267
268
# File 'lib/ronin/support/network/ip_range.rb', line 266

def to_s
  @range.to_s
end