Class: Inspec::Resources::LinuxInterface

Inherits:
InterfaceInfo show all
Defined in:
lib/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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/resources/interface.rb', line 65

def interface_info(iface)
  # will return "[mtu]\n1500\n[type]\n1"
  cmd = inspec.command("find /sys/class/net/#{iface}/ -maxdepth 1 -type f -exec sh -c 'echo \"[$(basename {})]\"; cat {} || echo -n' \\;")
  return nil if cmd.exit_status.to_i != 0

  # parse values, we only recieve values, therefore we threat them as keys
  params = SimpleConfig.new(cmd.stdout.chomp).params

  # abort if we got an empty result-set
  return nil if params.empty?

  # parse state
  state = false
  if params.key?('operstate')
    operstate, _value = params['operstate'].first
    state = operstate == 'up'
  end

  # parse speed
  speed = nil
  if params.key?('speed')
    speed, _value = params['speed'].first
    speed = convert_to_i(speed)
  end

  {
    name: iface,
    up: state,
    speed: speed,
  }
end