summaryrefslogtreecommitdiffstats
path: root/lib/facter
diff options
context:
space:
mode:
authorRein Henrichs <rein@puppetlabs.com>2010-08-11 11:13:59 -0700
committerRein Henrichs <rein@puppetlabs.com>2010-08-11 11:17:37 -0700
commit016cf037435154fc901ac93962f3ba464432c62c (patch)
treeb56878dae7e20ea80491a8cb7dd47ad6d77f18e7 /lib/facter
parent6edf3199a212e6b33f9e09edbb3cb34710710326 (diff)
downloadfacter-016cf037435154fc901ac93962f3ba464432c62c.tar.gz
facter-016cf037435154fc901ac93962f3ba464432c62c.tar.xz
facter-016cf037435154fc901ac93962f3ba464432c62c.zip
[#3703] Fix macaddress fact for Darwin
* With tests for 9.8.0, 10.3.0 and 10.6.4
Diffstat (limited to 'lib/facter')
-rw-r--r--lib/facter/macaddress.rb16
-rw-r--r--lib/facter/util/macaddress.rb28
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