Class: Inspec::Resources::WindowsHostProvider

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

Overview

Windows TODO: UDP is not supported yey, we need a custom ps1 script to add udp support

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

#ping(hostname, port = nil, _proto = nil) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/inspec/resources/host.rb', line 281

def ping(hostname, port = nil, _proto = nil)
  # ICMP: Test-NetConnection www.microsoft.com
  # TCP and port: Test-NetConnection -ComputerName www.microsoft.com -RemotePort 80
  request = "Test-NetConnection -ComputerName #{hostname} -WarningAction SilentlyContinue"
  request += " -RemotePort #{port}" unless port.nil?
  request += "| Select-Object -Property ComputerName, TcpTestSucceeded, PingSucceeded | ConvertTo-Json"
  cmd = inspec.command(request)

  begin
    ping = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return {}
  end

  { success: port.nil? ? ping["PingSucceeded"] : ping["TcpTestSucceeded"] }
end

#resolve(hostname) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/inspec/resources/host.rb', line 298

def resolve(hostname)
  cmd = inspec.command("Resolve-DnsName –Type A #{hostname} | ConvertTo-Json")
  begin
    resolv = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  resolv = [resolv] unless resolv.is_a?(Array)
  resolv.map { |entry| entry["IPAddress"] }
end