Class: Inspec::Resources::Interfaces

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/interfaces.rb

Defined Under Namespace

Classes: BsdInterfaceLister, InterfaceLister, LinuxInterfaceLister, WindowsInterfaceLister

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#iface_dataObject (readonly)

Returns the value of attribute iface_data.



16
17
18
# File 'lib/inspec/resources/interfaces.rb', line 16

def iface_data
  @iface_data
end

Instance Method Details

#ipv4_addressObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inspec/resources/interfaces.rb', line 26

def ipv4_address
  require "ipaddr" unless defined?(IPAddr)

  # Loop over interface names
  # Select those that are up and have an ipv4 address
  interfaces = names.map { |n| inspec.interface(n) }.select do |i|
    i.ipv4_address? && i.up?
  end

  addrs = interfaces.map(&:ipv4_addresses).flatten.map { |a| IPAddr.new(a) }

  # Look for progressively "better" IP addresses
  [
    # Loopback and private IP ranges
    IPAddr.new("127.0.0.0/8"),
    IPAddr.new("192.168.0.0/16"),
    IPAddr.new("172.16.0.0/12"),
    IPAddr.new("10.0.0.0/8"),
  ].each do |private_range|
    filtered_addrs = addrs.reject { |a| private_range.include?(a) }
    if filtered_addrs.empty?
      # Everything we had was a private or loopback IP. Return the "best" thing we were left with.
      return addrs.first.to_s
    end

    addrs = filtered_addrs
  end
  addrs.first.to_s
end

#to_sObject



18
19
20
# File 'lib/inspec/resources/interfaces.rb', line 18

def to_s
  "Interfaces"
end