Method: Inspec::Resources::WindowsHostProvider#ping

Defined in:
lib/resources/host.rb

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/resources/host.rb', line 223

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