Sha256: 28e86297ab8fdd4a0cca66ecab890617693a52676ed48d1a6a5499663a66172b

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Macosx
      class Ipaddress < BaseResolver
        @log = Facter::Log.new(self)
        @semaphore = Mutex.new
        @fact_list ||= {}
        class << self
          private

          def post_resolve(fact_name)
            @fact_list.fetch(fact_name) { read_ipaddress(fact_name) }
          end

          def read_ipaddress(fact_name)
            ip = nil
            primary_interface = read_primary_interface
            unless primary_interface.nil?
              @fact_list[:primary] = primary_interface
              output, _status = Open3.capture2("ipconfig getifaddr #{primary_interface}")
              ip = output.strip
            end
            find_all_interfaces
            @fact_list[:ip] = ip
            @fact_list[fact_name]
          end

          def read_primary_interface
            iface = nil
            output, _status = Open3.capture2('route -n get default')
            output.split(/^\S/).each do |str|
              iface = Regexp.last_match(1) if str.strip =~ /interface: (\S+)/
            end
            iface
          end

          def find_all_interfaces
            output, _status = Open3.capture2('ifconfig -a 2>/dev/null')

            data_hash = Hash[*output.split(/^([A-Za-z0-9_]+): /)[1..-1]]

            macaddress = */ether (([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}))/.match(data_hash[@fact_list[:primary]])
            @fact_list[:macaddress] = macaddress[1]

            @fact_list[:interfaces] = data_hash.keys.join(',')
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
facter-4.0.20 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.19 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.18 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.17 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.16 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.15 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.14 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.13 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.12 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.11 lib/resolvers/macosx/ipaddress_resolver.rb
facter-4.0.10.pre lib/resolvers/macosx/ipaddress_resolver.rb