Method: Inspec::Resources::WindowsHostProvider#ping

Defined in:
lib/inspec/resources/host.rb

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



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

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