Class: Inspec::Resources::WindowsInterface

Inherits:
InterfaceInfo show all
Defined in:
lib/inspec/resources/interface.rb

Instance Attribute Summary

Attributes inherited from InterfaceInfo

#inspec

Instance Method Summary collapse

Methods inherited from InterfaceInfo

#initialize

Methods included from Converter

#convert_to_i, to_boolean

Constructor Details

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

Instance Method Details

#interface_info(iface) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/inspec/resources/interface.rb', line 216

def interface_info(iface)
  # gather all network interfaces
  cmd = inspec.command("Get-NetAdapter | Select-Object -Property Name, InterfaceDescription, Status, State, " \
                       "MacAddress, LinkSpeed, ReceiveLinkSpeed, TransmitLinkSpeed, Virtual | ConvertTo-Json")

  addr_cmd = inspec.command("Get-NetIPAddress | Select-Object -Property IPv6Address, IPv4Address, InterfaceAlias," \
                            " PrefixLength | ConvertTo-Json")

  # filter network interface
  begin
    net_adapter = JSON.parse(cmd.stdout)
    addresses = JSON.parse(addr_cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  # ensure we have an array of groups
  net_adapter = [net_adapter] unless net_adapter.is_a?(Array)
  addresses = [addresses] unless addresses.is_a?(Array)

  # select the requested interface
  adapters = net_adapter.each_with_object([]) do |adapter, adapter_collection|
    # map object
    info = {
      name: adapter["Name"],
      up: adapter["State"] == 2,
      speed: adapter["ReceiveLinkSpeed"] / 1000,
      ipv4_addresses: addresses_for_proto(addresses, adapter["Name"], "IPv4"),
      ipv6_addresses: addresses_for_proto(addresses, adapter["Name"], "IPv6"),
    }
    adapter_collection.push(info) if info[:name].casecmp(iface) == 0
  end

  return nil if adapters.empty?

  warn "[Possible Error] detected multiple network interfaces with the name #{iface}" if adapters.size > 1
  adapters[0]
end