Class: IPAddr
- Includes:
- Enumerable
- Defined in:
- lib/ronin/extensions/ip_addr.rb
Constant Summary collapse
- MASKS =
Socket families and IP address masks
{ Socket::AF_INET => IN4MASK, Socket::AF_INET6 => IN6MASK }
Class Method Summary collapse
-
.each(cidr_or_glob) {|ip| ... } ⇒ nil
Iterates over each IP address within the IP Address range.
-
.extract(text, version = nil) {|ip| ... } ⇒ Array<String>
Extracts IP Addresses from text.
Instance Method Summary collapse
-
#each {|ip| ... } ⇒ Object
Iterates over each IP address that is included in the addresses netmask.
-
#lookup(nameserver = nil) ⇒ Array<String>
Resolves the host-names for the IP address.
Methods included from Enumerable
Class Method Details
.each(cidr_or_glob) {|ip| ... } ⇒ nil
Iterates over each IP address within the IP Address range. Supports both IPv4 and IPv6 address ranges.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ronin/extensions/ip_addr.rb', line 121 def IPAddr.each(cidr_or_glob,&block) unless (cidr_or_glob.include?('*') || cidr_or_glob.include?('-')) return IPAddr.new(cidr_or_glob).each(&block) end return enum_for(:each,cidr_or_glob) unless block if cidr_or_glob.include?('::') separator = '::' base = 16 prefix = if cidr_or_glob.start_with?('::') '::' else '' end format = lambda { |address| prefix + address.map { |i| '%.2x' % i }.join('::') } else separator = '.' base = 10 format = lambda { |address| address.join('.') } end # split the address segments = cidr_or_glob.split(separator) ranges = [] # map the components of the address to numeric ranges segments.each do |segment| next if segment.empty? ranges << if segment == '*' (1..254) elsif segment.include?('-') start, stop = segment.split('-',2) (start.to_i(base)..stop.to_i(base)) else segment.to_i(base) end end # cycle through the address ranges ranges.comprehension { |address| yield format[address] } return nil end |
.extract(text, version = nil) {|ip| ... } ⇒ Array<String>
Extracts IP Addresses from text.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ronin/extensions/ip_addr.rb', line 66 def IPAddr.extract(text,version=nil,&block) return enum_for(:extract,text,version).to_a unless block_given? regexp = case version when :ipv4, :v4, 4 Regexp::IPv4 when :ipv6, :v6, 6 Regexp::IPv6 else Regexp::IP end scanner = StringScanner.new(text) while scanner.skip_until(regexp) yield scanner.matched end return nil end |
Instance Method Details
#each {|ip| ... } ⇒ Object
Iterates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ronin/extensions/ip_addr.rb', line 206 def each return enum_for(:each) unless block_given? family_mask = MASKS[@family] (0..((~@mask_addr) & family_mask)).each do |i| yield _to_string(@addr | i) end return self end |