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

Constructor Details

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

Instance Method Details

#interface_info(iface) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/inspec/resources/interface.rb', line 159

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