diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/facter/macaddress.rb | 16 | ||||
-rw-r--r-- | lib/facter/util/macaddress.rb | 28 |
2 files changed, 31 insertions, 13 deletions
diff --git a/lib/facter/macaddress.rb b/lib/facter/macaddress.rb index 889feea..adc2c64 100644 --- a/lib/facter/macaddress.rb +++ b/lib/facter/macaddress.rb @@ -1,3 +1,5 @@ +require 'facter/util/macaddress' + Facter.add(:macaddress) do confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE SLES Debian Gentoo Ubuntu OEL OVS} setcode do @@ -26,19 +28,7 @@ end Facter.add(:macaddress) do confine :kernel => :darwin - setcode do - ether = nil - output = %x{/sbin/ifconfig} - - output.split(/^\S/).each do |str| - if str =~ /10baseT/ # we're wired - str =~ /ether (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/ - ether = $1 - end - end - - ether - end + setcode { Facter::Util::Macaddress::Darwin.macaddress } end Facter.add(:macaddress) do diff --git a/lib/facter/util/macaddress.rb b/lib/facter/util/macaddress.rb new file mode 100644 index 0000000..fc0a043 --- /dev/null +++ b/lib/facter/util/macaddress.rb @@ -0,0 +1,28 @@ +# A module to gather macaddress facts +# +module Facter::Util::Macaddress + + module Darwin + def self.macaddress + iface = default_interface + Facter.warn "Could not find a default route. Using first non-loopback interface" if iface.empty? + + macaddress = `#{ifconfig_command} #{iface} | /usr/bin/awk '/ether/{print $2;exit}'`.chomp + macaddress.empty? ? nil : macaddress + end + + def self.default_interface + `#{netstat_command} | /usr/bin/awk '/^default/{print $6}'`.chomp + end + + private + + def self.netstat_command + '/usr/sbin/netstat -rn' + end + + def self.ifconfig_command + '/sbin/ifconfig' + end + end +end |