Class: Inspec::Resources::UnixHostProvider

Inherits:
HostProvider show all
Defined in:
lib/resources/host.rb

Direct Known Subclasses

DarwinHostProvider, LinuxHostProvider

Instance Attribute Summary

Attributes inherited from HostProvider

#inspec

Instance Method Summary collapse

Methods inherited from HostProvider

#initialize, #missing_requirements

Constructor Details

This class inherits a constructor from Inspec::Resources::HostProvider

Instance Method Details

#resolve_with_dig(hostname) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/resources/host.rb', line 150

def resolve_with_dig(hostname)
  addresses = []

  # look for IPv4 addresses
  cmd = inspec.command("dig +short A #{hostname}")
  cmd.stdout.lines.each do |line|
    matched = line.chomp.match(Resolv::IPv4::Regex)
    addresses << matched.to_s unless matched.nil?
  end

  # look for IPv6 addresses
  cmd = inspec.command("dig +short AAAA #{hostname}")
  cmd.stdout.lines.each do |line|
    matched = line.chomp.match(Resolv::IPv6::Regex)
    addresses << matched.to_s unless matched.nil?
  end

  addresses.empty? ? nil : addresses
end

#resolve_with_getent(hostname) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/resources/host.rb', line 170

def resolve_with_getent(hostname)
  cmd = inspec.command("getent ahosts #{hostname}")
  return nil unless cmd.exit_status.to_i.zero?

  # getent ahosts output is formatted like so:
  # $ getent ahosts www.google.com
  # 172.217.8.4     STREAM www.google.com
  # 172.217.8.4     DGRAM
  # 172.217.8.4     RAW
  # 2607:f8b0:4004:803::2004 STREAM
  # 2607:f8b0:4004:803::2004 DGRAM
  # 2607:f8b0:4004:803::2004 RAW
  addresses = []
  cmd.stdout.lines.each do |line|
    ip, = line.split(/\s+/, 2)
    next unless ip.match(Resolv::IPv4::Regex) || ip.match(Resolv::IPv6::Regex)
    addresses << ip unless addresses.include?(ip)
  end

  addresses
end