class Facter::Util::Linux::RoutingTable

Constants

ROUTE_TYPES

Public Class Methods

read_routing_table(logger) click to toggle source
# File lib/facter/util/linux/routing_table.rb, line 18
def read_routing_table(logger)
  ipv4_output = Facter::Core::Execution.execute('ip route show', logger: logger)
  ipv6_output = Facter::Core::Execution.execute('ip -6 route show', logger: logger)
  routes4 = parse_routes(ipv4_output, true)
  routes6 = parse_routes(ipv6_output, false)
  [routes4, routes6]
end

Private Class Methods

construct_route(parts) click to toggle source
# File lib/facter/util/linux/routing_table.rb, line 48
def construct_route(parts)
  route = {}
  dev_index = parts.find_index { |elem| elem == 'dev' }
  src_index = parts.find_index { |elem| elem == 'src' }
  route[:interface] = parts[dev_index + 1] if dev_index
  route[:ip] = parts[src_index + 1] if src_index
  route
end
delete_invalid_route_type(parts) click to toggle source
# File lib/facter/util/linux/routing_table.rb, line 43
def delete_invalid_route_type(parts)
  route_type = parts[0]
  parts.delete_at(0) if ROUTE_TYPES.include?(route_type)
end
parse_routes(output, ipv4_type) click to toggle source
# File lib/facter/util/linux/routing_table.rb, line 28
def parse_routes(output, ipv4_type)
  routes = []
  output.each_line do |line|
    parts = line.split(' ').compact
    next if parts.include?('linkdown')

    delete_invalid_route_type(parts)
    next if !ipv4_type && !parts[0].include?(':')

    route = construct_route(parts)
    routes << route unless route[:ip].nil?
  end
  routes.uniq
end