PageRenderTime 76ms CodeModel.GetById 48ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/facter/ipaddress6.rb

https://github.com/ody/ody-ipaddress6
Ruby | 130 lines | 93 code | 19 blank | 18 comment | 11 complexity | 61ea386680e5e3c39029ea15c9449f89 MD5 | raw file
  1. # Cody Herriges <c.a.herriges@gmail.com>
  2. #
  3. # Used the ipaddress fact that is already part of
  4. # Facter as a template.
  5. # Uses ruby's own resolv class which queries DNS and /etc/hosts.
  6. # The closest thing to a default/primary IPv6 addresses is
  7. # assumed to be the AAAA that you have published via DNS or
  8. # an /etc/host entry.
  9. Facter.add(:ipaddress6, :timeout => 2) do
  10. setcode do
  11. require 'resolv'
  12. begin
  13. if fqdn = Facter.value(:fqdn)
  14. ip = nil
  15. Resolv.getaddresses(fqdn).each { |str|
  16. str = str.to_s
  17. if str =~ /(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4}/ and str != "::1"
  18. ip = str
  19. end
  20. }
  21. ip
  22. else
  23. nil
  24. end
  25. rescue Resolv::ResolvError
  26. nil
  27. rescue NoMethodError # i think this is a bug in resolv.rb?
  28. nil
  29. end
  30. end
  31. end
  32. # Uses the OS' host command to do a DNS lookup.
  33. Facter.add(:ipaddress6, :timeout => 2) do
  34. setcode do
  35. if fqdn = Facter.value(:fqdn)
  36. ip = nil
  37. host = nil
  38. if host = Facter::Util::Resolution.exec("host -t AAAA #{fqdn}")
  39. host.scan(/((?>[0-9,a-f,A-F]{0,4}\:{1,2})+[0-9,a-f,A-F]{0,4}&)/).each { |str|
  40. str = str.to_s
  41. unless str =~ /fe80.*/ or str == "::1"
  42. ip = str
  43. end
  44. }
  45. else
  46. nil
  47. end
  48. ip
  49. else
  50. nil
  51. end
  52. end
  53. end
  54. # OS dependant code that parses the output of various networking
  55. # tools and currently not very intelligent. Returns the first
  56. # non-loopback and non-linklocal address found in the ouput unless
  57. # a default route can be mapped to a routeable interface. Guessing
  58. # an interface is currently only possible with BSD type systems
  59. # to many assumptions have to be made on other platforms to make
  60. # this work with the current code. Most code ported or modeled
  61. # after the ipaddress fact for the sake of similar functionality
  62. # and familiar mechanics.
  63. Facter.add(:ipaddress6) do
  64. confine :kernel => :linux
  65. setcode do
  66. ip = nil
  67. output = Facter::Util::Resolution.exec("/sbin/ifconfig")
  68. output.scan(/inet6 addr: ((?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/).each { |str|
  69. str = str.to_s
  70. unless str =~ /fe80.*/ or str == "::1"
  71. ip = str
  72. end
  73. }
  74. ip
  75. end
  76. end
  77. Facter.add(:ipaddress6) do
  78. confine :kernel => %w{SunOS}
  79. setcode do
  80. output = Facter::Util::Resolution.exec("/usr/sbin/ifconfig -a")
  81. ip = nil
  82. output.scan(/inet6 ((?>[0-9,a-f,A-F]*\:{0,2})+[0-9,a-f,A-F]{0,4})/).each { |str|
  83. str = str.to_s
  84. unless str =~ /fe80.*/ or str == "::1"
  85. ip = str
  86. end
  87. }
  88. ip
  89. end
  90. end
  91. Facter.add(:ipaddress6) do
  92. confine :kernel => %w{Darwin FreeBSD OpenBSD}
  93. setcode do
  94. interout = Facter::Util::Resolution.exec("/usr/sbin/netstat -rn -f inet6")
  95. interface = interout.scan(/^default\s+fe80\S+\s+[A-Z]+\s+\d\s+\d+\s+([a-z]+\d)/).to_s
  96. if interface != ''
  97. output = Facter::Util::Resolution.exec("/sbin/ifconfig #{interface}")
  98. else
  99. puts "Unable to find a default route interface, using first non-loopback address"
  100. output = Facter::Util::Resolution.exec("/sbin/ifconfig -a")
  101. end
  102. ip = nil
  103. output.scan(/inet6 ((?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/).each { |str|
  104. str = str.to_s
  105. unless str =~ /fe80.*/ or str == "::1"
  106. ip = str
  107. end
  108. }
  109. ip
  110. end
  111. end