diff options
author | ajax <ajax@1f5c1d6a-bddf-0310-8f58-fc49e503516a> | 2007-01-30 18:17:03 +0000 |
---|---|---|
committer | ajax <ajax@1f5c1d6a-bddf-0310-8f58-fc49e503516a> | 2007-01-30 18:17:03 +0000 |
commit | 824f91cfa60cfe7b6bdc62ae498f033d1bda9634 (patch) | |
tree | 650d8545d980316b592f257de6fd2c5f3c7c16b5 | |
parent | 38cd6137bb5956b47ee5caffe6e071e28ad6d755 (diff) | |
download | facter-824f91cfa60cfe7b6bdc62ae498f033d1bda9634.tar.gz facter-824f91cfa60cfe7b6bdc62ae498f033d1bda9634.tar.xz facter-824f91cfa60cfe7b6bdc62ae498f033d1bda9634.zip |
Fixing bug where an up interface not in active use was being selected as the canonical IP instead of using the IP attached to the interface assigned the default route.
git-svn-id: http://reductivelabs.com/svn/facter/trunk@194 1f5c1d6a-bddf-0310-8f58-fc49e503516a
-rw-r--r-- | lib/facter.rb | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/facter.rb b/lib/facter.rb index 6d95ce9..fe0b684 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -938,7 +938,7 @@ class Facter end end Facter.add("IPAddress") do - confine :kernel => %w{FreeBSD NetBSD OpenBSD solaris darwin} + confine :kernel => %w{FreeBSD NetBSD OpenBSD solaris} setcode do ip = nil output = %x{/sbin/ifconfig} @@ -956,6 +956,36 @@ class Facter ip end end + Facter.add("IPAddress") do + confine :kernel => %w{darwin} + setcode do + ip = nil + iface = "" + output = %x{/usr/sbin/netstat -rn} + if output =~ /^default\s*\S*\s*\S*\s*\S*\s*\S*\s*(\S*).*/ + iface = $1 + else + warn "Could not find a default route. Using first non-loopback interface" + end + if(iface != "") + output = %x{/sbin/ifconfig #{iface}} + else + output = %x{/sbin/ifconfig} + end + + output.split(/^\S/).each { |str| + if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ + tmp = $1 + unless tmp =~ /127\./ + ip = tmp + break + end + end + } + + ip + end + end Facter.add("Hostname") do confine :kernel => :darwin, :kernelrelease => "R7" setcode do |