Facter::Util::IP

A base module for collecting IP-related information from all kinds of platforms.

Constants

REGEX_MAP

A map of all the different regexes that work for a given platform or set of platforms.

Public Class Methods

alphafy(interface) click to toggle source

Convert an interface name into purely alphanumeric characters.

    # File lib/facter/util/ip.rb, line 39
39:   def self.alphafy(interface)
40:     interface.gsub(/[^a-z0-9_]/, '_')
41:   end
convert_from_hex?(kernel) click to toggle source
    # File lib/facter/util/ip.rb, line 43
43:   def self.convert_from_hex?(kernel)
44:     kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin, :"hp-ux", :"gnu/kfreebsd", :dragonfly]
45:     kernels_to_convert.include?(kernel)
46:   end
get_all_interface_output() click to toggle source
    # File lib/facter/util/ip.rb, line 74
74:   def self.get_all_interface_output
75:     case Facter.value(:kernel)
76:     when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin', 'GNU/kFreeBSD', 'DragonFly'
77:       output = %{/sbin/ifconfig -a}
78:     when 'SunOS'
79:       output = %{/usr/sbin/ifconfig -a}
80:     when 'HP-UX'
81:       output = %{/bin/netstat -in | sed -e 1d}
82:     when 'windows'
83:       output = %#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ip show interface|
84:       output += %#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ipv6 show interface|
85:     end
86:     output
87:   end
get_arp_value(interface) click to toggle source
     # File lib/facter/util/ip.rb, line 196
196:   def self.get_arp_value(interface)
197:     arp = Facter::Util::Resolution.exec("arp -en -i #{interface} | sed -e 1d")
198:     if arp =~ /^\S+\s+\w+\s+(\S+)\s+\w\s+\S+$/
199:      return $1
200:     end
201:   end
get_bonding_master(interface) click to toggle source
     # File lib/facter/util/ip.rb, line 117
117:   def self.get_bonding_master(interface)
118:     if Facter.value(:kernel) != 'Linux'
119:       return nil
120:     end
121:     # We need ip instead of ifconfig because it will show us
122:     # the bonding master device.
123:     if not FileTest.executable?("/sbin/ip")
124:       return nil
125:     end
126:     # A bonding interface can never be an alias interface. Alias
127:     # interfaces do have a colon in their name and the ip link show
128:     # command throws an error message when we pass it an alias
129:     # interface.
130:     if interface =~ /:/
131:       return nil
132:     end
133:     regex = /SLAVE[,>].* (bond[0-9]+)/
134:       ethbond = regex.match(%{/sbin/ip link show #{interface}})
135:     if ethbond
136:       device = ethbond[1]
137:     else
138:       device = nil
139:     end
140:     device
141:   end
get_interface_value(interface, label) click to toggle source
     # File lib/facter/util/ip.rb, line 143
143:   def self.get_interface_value(interface, label)
144:     tmp1 = []
145: 
146:     kernel = Facter.value(:kernel).downcase.to_sym
147: 
148:     # If it's not directly in the map or aliased in the map, then we don't know how to deal with it.
149:     unless map = REGEX_MAP[kernel] || REGEX_MAP.values.find { |tmp| tmp[:aliases] and tmp[:aliases].include?(kernel) }
150:       return []
151:     end
152: 
153:     # Pull the correct regex out of the map.
154:     regex = map[label.to_sym]
155: 
156:     # Linux changes the MAC address reported via ifconfig when an ethernet interface
157:     # becomes a slave of a bonding device to the master MAC address.
158:     # We have to dig a bit to get the original/real MAC address of the interface.
159:     bonddev = get_bonding_master(interface)
160:     if label == 'macaddress' and bonddev
161:       bondinfo = IO.readlines("/proc/net/bonding/#{bonddev}")
162:       hwaddrre = /^Slave Interface: #{interface}\n[^\n].+?\nPermanent HW addr: (([0-9a-fA-F]{2}:?)*)$/
163:       value = hwaddrre.match(bondinfo.to_s)[1].upcase
164:     else
165:       output_int = get_output_for_interface_and_label(interface, label)
166: 
167:       output_int.each_line do |s|
168:         if s =~ regex
169:           value = $1
170:             if label == 'netmask' && convert_from_hex?(kernel)
171:               value = value.scan(/../).collect do |byte| byte.to_i(16) end.join('.')
172:             end
173:           tmp1.push(value)
174:         end
175:       end
176: 
177:       if tmp1
178:         value = tmp1.shift
179:       end
180:     end
181:   end
get_interfaces() click to toggle source
    # File lib/facter/util/ip.rb, line 60
60:   def self.get_interfaces
61:     return [] unless output = Facter::Util::IP.get_all_interface_output()
62: 
63:     # windows interface names contain spaces and are quoted and can appear multiple
64:     # times as ipv4 and ipv6
65:     return output.scan(/\s* connected\s*(\S.*)/).flatten.uniq if Facter.value(:kernel) == 'windows'
66: 
67:     # Our regex appears to be stupid, in that it leaves colons sitting
68:     # at the end of interfaces.  So, we have to trim those trailing
69:     # characters.  I tried making the regex better but supporting all
70:     # platforms with a single regex is probably a bit too much.
71:     output.scan(/^\S+/).collect { |i| i.sub(/:$/, '') }.uniq
72:   end
get_network_value(interface) click to toggle source
     # File lib/facter/util/ip.rb, line 183
183:   def self.get_network_value(interface)
184:     require 'ipaddr'
185: 
186:     ipaddress = get_interface_value(interface, "ipaddress")
187:     netmask = get_interface_value(interface, "netmask")
188: 
189:     if ipaddress && netmask
190:       ip = IPAddr.new(ipaddress, Socket::AF_INET)
191:       subnet = IPAddr.new(netmask, Socket::AF_INET)
192:       network = ip.mask(subnet.to_s).to_s
193:     end
194:   end
get_output_for_interface_and_label(interface, label) click to toggle source
     # File lib/facter/util/ip.rb, line 106
106:   def self.get_output_for_interface_and_label(interface, label)
107:     return get_single_interface_output(interface) unless Facter.value(:kernel) == 'windows'
108: 
109:     if label == 'ipaddress6'
110:       output = %#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ipv6 show address \"#{interface}\"|
111:     else
112:       output = %#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ip show address \"#{interface}\"|
113:     end
114:     output
115:   end
get_single_interface_output(interface) click to toggle source
     # File lib/facter/util/ip.rb, line 89
 89:   def self.get_single_interface_output(interface)
 90:     output = ""
 91:     case Facter.value(:kernel)
 92:     when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin', 'GNU/kFreeBSD', 'DragonFly'
 93:       output = %{/sbin/ifconfig #{interface}}
 94:     when 'SunOS'
 95:       output = %{/usr/sbin/ifconfig #{interface}}
 96:     when 'HP-UX'
 97:        mac = ""
 98:        ifc = %{/usr/sbin/ifconfig #{interface}}
 99:        %{/usr/sbin/lanscan}.scan(/(\dx\S+).*UP\s+(\w+\d+)/).each {|i| mac = i[0] if i.include?(interface) }
100:        mac = mac.sub(/0x(\S+)/,'\1').scan(/../).join(":")
101:        output = ifc + "\n" + mac
102:     end
103:     output
104:   end
supported_platforms() click to toggle source
    # File lib/facter/util/ip.rb, line 48
48:   def self.supported_platforms
49:     REGEX_MAP.inject([]) do |result, tmp|
50:       key, map = tmp
51:       if map[:aliases]
52:         result += map[:aliases]
53:       else
54:         result << key
55:       end
56:       result
57:     end
58:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.