Class: Inspec::Resources::WindowsHostProvider

Inherits:
HostProvider
  • Object
show all
Defined in:
lib/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

Constructor Details

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

Instance Method Details

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/resources/host.rb', line 117

def ping(hostname, port = nil, proto = nil)
  # TODO: abort if we cannot run it via udp
  return nil if proto == 'udp'

  # ICMP: Test-NetConnection www.microsoft.com
  # TCP and port: Test-NetConnection -ComputerName www.microsoft.com -RemotePort 80
  request = "Test-NetConnection -ComputerName #{hostname}"
  request += " -RemotePort #{port}" unless port.nil?
  request += '| Select-Object -Property ComputerName, RemoteAddress, RemotePort, SourceAddress, PingSucceeded | ConvertTo-Json'
  p request
  request += '| Select-Object -Property ComputerName, PingSucceeded | ConvertTo-Json'
  cmd = inspec.command(request)

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

  ping['PingSucceeded']
end

#resolve(hostname) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/resources/host.rb', line 139

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