Class: IPAddr

Inherits:
Object show all
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

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

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.

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:

  • cidr_or_glob (String)

    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:

  • ip (String)

    An IP address within the IP address range.

Returns:

  • (nil)


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.

Examples:

IPAddr.extract("Host: 127.0.0.1\n\rHost: 10.1.1.1\n\r")
# => ["127.0.0.1", "10.1.1.1"]

Extract only IPv4 addresses from a large amount of text.

IPAddr.extract(text,:v4) do |ip|
  puts ip
end

Parameters:

  • text (String)

    The text to scan for IP Addresses.

  • version (Integer, Symbol) (defaults to: nil)

    The version of IP Address to scan for (4, 6, :v4 or :v6).

Yields:

  • (ip)

    The given block will be passed each extracted IP Address.

Yield Parameters:

  • ip (String)

    An IP Address from the text.

Returns:

  • (Array<String>)

    The IP Addresses found in the 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.

Examples:

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:

  • ip (String)

    An IP address.



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

#lookup(nameserver = nil) ⇒ Array<String>

Resolves the host-names for the IP address.

Parameters:

  • nameserver (String) (defaults to: nil)

    The optional nameserver to query.

Returns:

  • (Array<String>)

    The host-names for the IP address.



182
183
184
# File 'lib/ronin/extensions/ip_addr.rb', line 182

def lookup(nameserver=nil)
  Resolv.resolver(nameserver).getnames(self.to_s)
end