From 824f91cfa60cfe7b6bdc62ae498f033d1bda9634 Mon Sep 17 00:00:00 2001 From: ajax Date: Tue, 30 Jan 2007 18:17:03 +0000 Subject: 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 --- lib/facter.rb | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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 -- cgit