Class: IPAddr

Included Modules

Enumerable

Public Visibility

Public Class Method Summary

each(cidr_or_glob, &block) {|ip| ... }

Iterates over each IP address within the IP Address range.

Public Instance Method Summary

#each(&block) {|ip| ... }

Iterates over each IP address that is included in the addresses netmask.

Public Class Method Details

each

public each(cidr_or_glob, &block) {|ip| ... }

Iterates over each IP address within the IP Address range. Supports both IPv4 and IPv6 address ranges.

Meta Tags

Examples

Enumerate through a CIDR range

  IPAddr.each('10.1.1.1/24') do |ip|
    puts ip
  end

Enumerate through a globbed IP range

  IPAddr.each('10.1.1-5.*') do |ip|
    puts ip
  end

Enumerate through a globbed IPv6 range

  IPAddr.each('::ff::02-0a::c3') do |ip|
    puts ip
  end

Parameters:

[String] cidr_or_glob

The IP address range to iterate over. May be in standard CIDR notation or globbed format.

Yields:

[ip]

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

Yield Parameters:

[String] ip

An IP address within the IP address range.

[View source]


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ronin/extensions/ip_addr.rb', line 57

def IPAddr.each(cidr_or_glob,&block)
  unless (cidr_or_glob.include?('*') || cidr_or_glob.include?('-'))
    IPAddr.new(cidr_or_glob).each(&block)
    return nil
  end

  if cidr_or_glob.include?('::')
    prefix = if cidr_or_glob =~ /^::/
               '::'
             else
               ''
             end

    separator = '::'
    base = 16

    format = lambda { |address|
      prefix + address.map { |i| '%.2x' % i }.join('::')
    }
  else
    separator = '.'
    base = 10

    format = lambda { |address| address.join('.') }
  end

  ranges = cidr_or_glob.split(separator).map { |segment|
    if segment == '*'
      (1..254)
    elsif segment.include?('-')
      start, stop = segment.split('-',2).map { |i| i.to_i(base) }

      (start..stop)
    elsif !(segment.empty?)
      segment.to_i(base)
    end
  }.compact

  expand_range = lambda { |address,remaining|
    if remaining.empty?
      block.call(format.call(address))
    else
      n = remaining.first
      remaining = remaining[1..-1]

      if n.kind_of?(Range)
        n.each { |i| expand_range.call(address + [i], remaining) }
      else
        expand_range.call(address + [n], remaining)
      end
    end
  }

  expand_range.call([], ranges)
  return nil
end

Public Instance Method Details

each

public each(&block) {|ip| ... }

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

Meta Tags

Example:

  netblock = IPAddr.new('10.1.1.1/24')

  netblock.each do |ip|
    puts ip
  end

Yields:

[ip]

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

Yield Parameters:

[String] ip

An IP address.

[View source]


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ronin/extensions/ip_addr.rb', line 132

def each(&block)
  case @family
  when Socket::AF_INET
    family_mask = IN4MASK
  when Socket::AF_INET6
    family_mask = IN6MASK
  end

  (0..((~@mask_addr) & family_mask)).each do |i|
    block.call(_to_string(@addr | i))
  end

  return self
end
Generated on Friday, September 25 2009 at 02:57:21 PM by YARD 0.2.3.5 (ruby-1.8.6).