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

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/network/ip_range/glob.rb

Overview

Represents an IP-glob range.

Examples

ip_range = IPRange::Glob.new('10.0.1-3.*/24')
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

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(string) ⇒ Glob

Initializes and parses the IP-glob range.

Parameters:

  • string (String)

    The IP-glob string to parse.

Since:

  • 1.0.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ronin/support/network/ip_range/glob.rb', line 69

def initialize(string)
  @string = string

  if @string.include?(':') # IPv6
    @version   = 6
    @separator = ':'
    @base      = 16
    @formatter = method(:format_ipv6_address)
  else # IPv4
    @version   = 4
    @separator = '.'
    @base      = 10
    @formatter = method(:format_ipv4_address)
  end

  @ranges = @string.split(@separator).map do |segment|
    case segment
    when '*' then (1..254)
    when /,/ then parse_list(segment)
    when /-/ then parse_range(segment)
    else          [segment]
    end
  end
end

Instance Attribute Details

#stringString (readonly)

The IP glob string.

Returns:

Since:

  • 1.0.0



61
62
63
# File 'lib/ronin/support/network/ip_range/glob.rb', line 61

def string
  @string
end

Class Method Details

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

Enumerates over the IP-glob range.

Examples:

Enumerate through a IPv4 glob range:

IPRange::Glob.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

Enumerate through a globbed IPv6 range:

IPRange::Glob.each('::ff::02-0a::c3') { |ip| puts ip }

Parameters:

  • string (String)

    The IP-glob string to parse and enumerate over.

Yields:

  • (ip)

    The block which will be passed each IP address contained within the IP address range.

Yield Parameters:

  • ip (String)

    An IP address within the IP address range.

Returns:

  • (self)

Since:

  • 1.0.0



147
148
149
# File 'lib/ronin/support/network/ip_range/glob.rb', line 147

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

.parse(string) ⇒ Glob

Alias for new.

Parameters:

  • string (String)

    The IP-glob string to parse.

Returns:

  • (Glob)

    The parsed IP-glob range.

See Also:

Since:

  • 1.0.0



105
106
107
# File 'lib/ronin/support/network/ip_range/glob.rb', line 105

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

Instance Method Details

#each {|ip| ... } ⇒ self

Enumerates over the IP-glob range.

Examples:

Enumerate through a IPv4 glob range:

ip_range = IPRange::Glob.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

Enumerate through a globbed IPv6 range:

ip_range = IPRange::Glob.new('::ff::02-0a::c3')
ip_range.each { |ip| puts ip }

Yields:

  • (ip)

    The block which will be passed each IP address contained within the IP address range.

Yield Parameters:

  • ip (String)

    An IP address within the IP address range.

Returns:

  • (self)

Since:

  • 1.0.0



206
207
208
209
210
211
212
213
214
215
# File 'lib/ronin/support/network/ip_range/glob.rb', line 206

def each
  return enum_for(__method__) unless block_given?

  # cycle through the address ranges
  @ranges.comprehension do |address|
    yield @formatter.call(address)
  end

  return self
end

#inspectString

Inspects the IP-glob range.

Returns:

Since:

  • 1.0.0



231
232
233
# File 'lib/ronin/support/network/ip_range/glob.rb', line 231

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

#ipv4?Boolean

Determines if the IP-glob range is IPv4.

Returns:

  • (Boolean)

Since:

  • 1.0.0



156
157
158
# File 'lib/ronin/support/network/ip_range/glob.rb', line 156

def ipv4?
  @version == 4
end

#ipv6?Boolean

Determines if the IP-glob range is IPv6.

Returns:

  • (Boolean)

Since:

  • 1.0.0



165
166
167
# File 'lib/ronin/support/network/ip_range/glob.rb', line 165

def ipv6?
  @version == 6
end

#to_sString

Converts the IP-glob range back into a String.

Returns:

Since:

  • 1.0.0



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

def to_s
  @string
end