Class: Inspec::Resources::LinuxHostProvider

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

Instance Attribute Summary

Attributes inherited from HostProvider

#inspec

Instance Method Summary collapse

Methods inherited from HostProvider

#initialize

Constructor Details

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

Instance Method Details

#missing_requirements(protocol) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/resources/host.rb', line 182

def missing_requirements(protocol)
  missing = []

  if protocol == 'tcp'
    missing << 'netcat must be installed' unless inspec.command('nc').exist?
  end

  missing
end

#ping(hostname, port, protocol) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/resources/host.rb', line 192

def ping(hostname, port, protocol)
  if protocol == 'tcp'
    resp = inspec.command("echo | nc -v -w 1 #{hostname} #{port}")
  else
    # fall back to ping, but we can only test ICMP packages with ping
    resp = inspec.command("ping -w 1 -c 1 #{hostname}")
  end

  {
    success: resp.exit_status.to_i.zero?,
    connection: resp.stderr,
    socket: resp.stdout,
  }
end

#resolve(hostname) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/resources/host.rb', line 207

def resolve(hostname)
  # TODO: we rely on getent hosts for now, but it prefers to return IPv6, only then IPv4
  cmd = inspec.command("getent hosts #{hostname}")
  return nil if cmd.exit_status.to_i != 0

  # extract ip adress
  resolve = /^\s*(?<ip>\S+)\s+(.*)\s*$/.match(cmd.stdout.chomp)
  [resolve[1]] if resolve
end