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



148
149
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 148

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}"
  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 nil
  end

  # Logic being if you provided a port you wanted to check it was open
  if port.nil?
    ping['PingSucceeded']
  else
    ping['TcpTestSucceeded']
  end
end

#resolve(hostname) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/resources/host.rb', line 170

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