My preliminary work on a new fact that reports IPv6 addresses.  Since IPv6 
interfaces have a link local address even after obtaining a routable address 
the original ipaddress fact code that I had simply modified slightly isn't 
going to work the way I want it to. Everything but the Darwin section at the 
bottom is the old code I originally submitted. I only have OSX, Linux, and 
Solaris to test code on.


Signed-off-by: Cody Herriges <[email protected]>
---
 lib/facter/ipaddress6.rb |   95 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)
 create mode 100644 lib/facter/ipaddress6.rb

diff --git a/lib/facter/ipaddress6.rb b/lib/facter/ipaddress6.rb
new file mode 100644
index 0000000..457f16f
--- /dev/null
+++ b/lib/facter/ipaddress6.rb
@@ -0,0 +1,95 @@
+Facter.add(:ipaddress6, :ldapname => "iphostnumber", :timeout => 2) do
+    setcode do
+        require 'resolv'
+
+        begin
+            if hostname = Facter.value(:hostname)
+                ip = Resolv.getaddresses(hostname)
+                unless ip == "::1" or ip !=~ /(?>[0-9,a-f,A-F]+\:{0,2})+/
+                    ip
+                end
+            else
+                nil
+            end
+        rescue Resolv::ResolvError
+            nil
+        rescue NoMethodError # i think this is a bug in resolv.rb?
+            nil
+        end
+    end
+end
+
+Facter.add(:ipaddress6, :timeout => 2) do
+    setcode do
+        if hostname = Facter.value(:hostname)
+            # we need Hostname to exist for this to work
+            host = nil
+            if host = Facter::Util::Resolution.exec("host #{hostname}")
+                host = host.chomp.split(/\s/)
+                if defined? list[-1] and
+                        list[-1] =~ /(?>[0-9,a-f,A-F]+\:{0,2})+/
+                    list[-1]
+                end
+            else
+                nil
+            end
+        else
+            nil
+        end
+    end
+end
+
+Facter.add(:ipaddress6) do
+    confine :kernel => :linux
+    setcode do
+        ip = nil
+        output = %x{/sbin/ifconfig}
+
+        output.split(/^\S/).each { |str|
+            if str =~ /inet6 addr: (?>[0-9,a-f,A-F]+\:{0,2})+/
+                tmp = $1
+                unless tmp =~ /fe80\:/ or tmp =~ /\:\:1/
+                    ip = tmp
+                    break
+                end
+            end
+        }
+
+        ip
+    end
+end
+
+Facter.add(:ipaddress6) do
+    confine :kernel => %w{SunOS}
+    setcode do
+       ip = nil
+       output = %x{/usr/sbin/ifconfig -a}
+
+       output.split(/^\S/).each { |str|
+       if str =~ /inet6 (?>[0-9,a-f,A-F]+\:{0,2})+/
+                tmp = $1
+                unless tmp =~ /fe80\:/ or tmp =~ /\:\:1/
+                    ip = tmp
+                end
+            end
+        }
+
+        ip
+    end
+end
+
+Facter.add(:ipaddress6) do
+    confine :kernel => %w{Darwin}
+    setcode do
+       ip = nil
+       output = %x{/sbin/ifconfig -a}
+
+       tmp = output.scan(/inet6 ((?>[0-9,a-f,A-F]+\:{0,2})+)/).each { |str|
+                unless str =~ /fe80\:/ or str =~ /\:\:1/
+                    ip = str
+                end
+        }
+
+        ip
+    end
+end
-- 
1.6.6.1


-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to