Class: Inspec::Resources::BsdInterface

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/inspec/resources/interface.rb', line 115

def interface_info(iface)
  cmd = inspec.command("ifconfig #{iface}")
  return nil if cmd.exit_status.to_i != 0

  lines = cmd.stdout.split("\n")
  iface_info = {
    name: iface,
    ipv4_addresses: [], # Actually CIDRs
    ipv6_addresses: [], # are expected to go here
  }

  iface_info[:up] = lines[0].include?("UP")
  lines.each do |line|
    # IPv4 case
    m = line.match(/^\s+inet\s+((?:\d{1,3}\.){3}\d{1,3})\s+netmask\s+(0x[a-f0-9]{8})/)
    if m
      ip = m[1]
      hex_mask = m[2]
      cidr = hex_mask.to_i(16).to_s(2).count("1")
      iface_info[:ipv4_addresses] << "#{ip}/#{cidr}"
      next
    end

    # IPv6 case
    m = line.match(/^\s+inet6\s+([a-f0-9:]+)%#{iface}\s+prefixlen\s+(\d+)/)
    if m
      ip = m[1]
      cidr = m[2]
      iface_info[:ipv6_addresses] << "#{ip}/#{cidr}"
      next
    end

    # Speed detect, crummy - can't detect wifi, finds any number in the string
    # Ethernet autoselect (1000baseT <full-duplex>)
    m = line.match(/^\s+media:\D+(\d+)/)
    if m
      iface_info[:speed] = m[1].to_i
      next
    end
  end

  iface_info
end