diff options
-rw-r--r-- | lib/facter/interfaces.rb | 2 | ||||
-rw-r--r-- | lib/facter/ipaddress6.rb | 68 | ||||
-rw-r--r-- | lib/facter/util/ip.rb | 3 |
3 files changed, 72 insertions, 1 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/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})/ }, |