Method: Inspec::Resources::WindowsHostProvider#ping

Defined in:
lib/resources/host.rb

#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