diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/facter/interfaces.rb | 2 | ||||
-rw-r--r-- | lib/facter/ipaddress.rb | 33 | ||||
-rw-r--r-- | lib/facter/ipaddress6.rb | 68 | ||||
-rw-r--r-- | lib/facter/util/ip.rb | 3 |
4 files changed, 73 insertions, 33 deletions
diff --git a/lib/facter/interfaces.rb b/lib/facter/interfaces.rb index 1239215..4fbaef1 100644 --- a/lib/facter/interfaces.rb +++ b/lib/facter/interfaces.rb @@ -22,7 +22,7 @@ Facter::Util::IP.get_interfaces.each do |interface| # Make a fact for each detail of each interface. Yay. # There's no point in confining these facts, since we wouldn't be able to create # them if we weren't running on a supported platform. - %w{ipaddress macaddress netmask}.each do |label| + %w{ipaddress ipaddress6 macaddress netmask}.each do |label| Facter.add(label + "_" + Facter::Util::IP.alphafy(interface)) do setcode do Facter::Util::IP.get_interface_value(interface, label) diff --git a/lib/facter/ipaddress.rb b/lib/facter/ipaddress.rb index a08f26b..08a5dc8 100644 --- a/lib/facter/ipaddress.rb +++ b/lib/facter/ipaddress.rb @@ -19,7 +19,7 @@ Facter.add(:ipaddress) do end Facter.add(:ipaddress) do - confine :kernel => %w{FreeBSD OpenBSD} + confine :kernel => %w{FreeBSD OpenBSD Darwin} setcode do ip = nil output = %x{/sbin/ifconfig} @@ -59,37 +59,6 @@ Facter.add(:ipaddress) do 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(:ipaddress) do confine :kernel => %w{AIX} setcode do ip = nil diff --git a/lib/facter/ipaddress6.rb b/lib/facter/ipaddress6.rb new file mode 100644 index 0000000..547d636 --- /dev/null +++ b/lib/facter/ipaddress6.rb @@ -0,0 +1,68 @@ +# Cody Herriges <c.a.herriges@gmail.com> +# +# Used the ipaddress fact that is already part of +# Facter as a template. + +# OS dependant code that parses the output of various networking +# tools and currently not very intelligent. Returns the first +# non-loopback and non-linklocal address found in the ouput unless +# a default route can be mapped to a routeable interface. Guessing +# an interface is currently only possible with BSD type systems +# to many assumptions have to be made on other platforms to make +# this work with the current code. Most code ported or modeled +# after the ipaddress fact for the sake of similar functionality +# and familiar mechanics. +Facter.add(:ipaddress6) do + confine :kernel => :linux + setcode do + ip = nil + output = Facter::Util::Resolution.exec('/sbin/ifconfig') + + output.scan(/inet6 addr: ((?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/).each { |str| + str = str.to_s + unless str =~ /fe80.*/ or str == "::1" + ip = str + end + } + + ip + + end +end + +Facter.add(:ipaddress6) do + confine :kernel => %w{SunOS} + setcode do + output = Facter::Util::Resolution.exec('/usr/sbin/ifconfig -a') + ip = nil + + output.scan(/inet6 ((?>[0-9,a-f,A-F]*\:{0,2})+[0-9,a-f,A-F]{0,4})/).each { |str| + str = str.to_s + unless str =~ /fe80.*/ or str == "::1" + ip = str + end + } + + ip + + end +end + +Facter.add(:ipaddress6) do + confine :kernel => %w{Darwin FreeBSD OpenBSD} + setcode do + output = Facter::Util::Resolution.exec('/sbin/ifconfig -a') + ip = nil + + output.scan(/inet6 ((?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/).each do |str| + str = str.to_s + unless str =~ /fe80.*/ or str == "::1" + ip = str + break + end + end + + ip + end +end + diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb index 62d50a4..23eeb9c 100644 --- a/lib/facter/util/ip.rb +++ b/lib/facter/util/ip.rb @@ -6,17 +6,20 @@ module Facter::Util::IP REGEX_MAP = { :linux => { :ipaddress => /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, + :ipaddress6 => /inet6 addr: ((?![fe80|::1])(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/, :macaddress => /(?:ether|HWaddr)\s+(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/, :netmask => /Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ }, :bsd => { :aliases => [:openbsd, :netbsd, :freebsd, :darwin, :"gnu/kfreebsd"], :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, + :ipaddress6 => /inet6 ((?![fe80|::1])(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/, :macaddress => /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/, :netmask => /netmask\s+0x(\w{8})/ }, :sunos => { :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, + :ipaddress6 => /inet6 ((?![fe80|::1])(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/, :macaddress => /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/, :netmask => /netmask\s+(\w{8})/ }, |