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.
[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 = 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| .call(address + [i], remaining) } else .call(address + [n], remaining) end end } .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.
[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 |