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



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

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



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/inspec/resources/host.rb', line 312

def resolve(hostname)
  addresses = []
  # -Type A is the DNS query for IPv4 server Address.
  cmd = inspec.command("Resolve-DnsName –Type A #{hostname} | ConvertTo-Json")
  begin
    resolve_ipv4 = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  resolve_ipv4 = resolve_ipv4.inject(:merge) if resolve_ipv4.is_a?(Array)

  # Append the ipv4 addresses
  resolve_ipv4.each_value do |ip|
    matched = ip.to_s.chomp.match(Resolv::IPv4::Regex)
    next if matched.nil? || addresses.include?(matched.to_s)

    addresses << matched.to_s
  end

  # -Type AAAA is the DNS query for IPv6 server Address.
  cmd = inspec.command("Resolve-DnsName –Type AAAA #{hostname} | ConvertTo-Json")
  begin
    resolve_ipv6 = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  resolve_ipv6 = resolve_ipv6.inject(:merge) if resolve_ipv6.is_a?(Array)

  # Append the ipv6 addresses
  resolve_ipv6.each_value do |ip|
    matched = ip.to_s.chomp.match(Resolv::IPv6::Regex)
    next if matched.nil? || addresses.include?(matched.to_s)

    addresses << matched.to_s
  end

  addresses
end