Module: Facter::Util::Resolvers::Networking::PrimaryInterface

Defined in:
lib/facter/util/resolvers/networking/primary_interface.rb

Overview

Since:

  • 2.0.0

Constant Summary collapse

ROUTE_TABLE_MAPPING =

Since:

  • 2.0.0

{
  'Iface' => 0,
  'Destination' => 1,
  'Gateway' => 2,
  'Flags' => 3,
  'RefCnt' => 4,
  'Use' => 5,
  'Metric' => 6,
  'Mask' => 7,
  'MTU' => 8,
  'Window' => 9,
  'IRTT' => 10
}.freeze

Class Method Summary collapse

Class Method Details

.find_in_interfaces(interfaces) ⇒ Object

Since:

  • 2.0.0



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/facter/util/resolvers/networking/primary_interface.rb', line 59

def find_in_interfaces(interfaces)
  interfaces.each do |iface_name, interface|
    interface[:bindings]&.each do |binding|
      return iface_name unless Facter::Util::Resolvers::Networking.ignored_ip_address(binding[:address])
    end

    next unless interface[:bindings6]

    interface[:bindings6].each do |binding|
      return iface_name unless Facter::Util::Resolvers::Networking.ignored_ip_address(binding[:address])
    end
  end

  nil
end

.read_from_ip_routeObject

Since:

  • 2.0.0



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/facter/util/resolvers/networking/primary_interface.rb', line 47

def read_from_ip_route
  return if Facter::Core::Execution.which('ip').nil?

  output = Facter::Core::Execution.execute('ip route show default', logger: @log)
  primary_interface = nil
  output.each_line do |line|
    primary_interface = line.strip.split(' ')[4] if line.start_with?('default')
  end

  primary_interface
end

.read_from_proc_routeObject

Since:

  • 2.0.0



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/facter/util/resolvers/networking/primary_interface.rb', line 33

def read_from_proc_route
  content = Facter::Util::FileHelper.safe_read('/proc/net/route', '')

  content.each_line.with_index do |line, index|
    next if index.zero?

    route = line.strip.split("\t")

    return route[ROUTE_TABLE_MAPPING['Iface']] if valid_default_route?(route)
  end

  nil
end

.read_from_routeObject

Since:

  • 2.0.0



25
26
27
28
29
30
31
# File 'lib/facter/util/resolvers/networking/primary_interface.rb', line 25

def read_from_route
  return if Facter::Core::Execution.which('route').nil?

  result = Facter::Core::Execution.execute('route -n get default', logger: @log)

  result.match(/interface: (.+)/)&.captures&.first
end