| Module | Facter::Util::IP |
| In: |
lib/facter/util/ip.rb
|
A base module for collecting IP-related information from all kinds of platforms.
| REGEX_MAP | = | { :linux => { :ipaddress => /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, :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]+)/ | A map of all the different regexes that work for a given platform or set of platforms. |
Convert an interface name into purely alpha characters.
# File lib/facter/util/ip.rb, line 26
26: def self.alphafy(interface)
27: interface.gsub(/[:.]/, '_')
28: end
# File lib/facter/util/ip.rb, line 30
30: def self.convert_from_hex?(kernel)
31: kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin]
32: kernels_to_convert.include?(kernel)
33: end
# File lib/facter/util/ip.rb, line 61
61: def self.get_all_interface_output
62: case Facter.value(:kernel)
63: when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin'
64: output = %x{/sbin/ifconfig -a}
65: when 'SunOS'
66: output = %x{/usr/sbin/ifconfig -a}
67: end
68: output
69: end
# File lib/facter/util/ip.rb, line 82
82: def self.get_bonding_master(interface)
83: if Facter.value(:kernel) != 'Linux'
84: return nil
85: end
86: # We need ip instead of ifconfig because it will show us
87: # the bonding master device.
88: if not FileTest.executable?("/sbin/ip")
89: return nil
90: end
91: regex = /SLAVE[,>].* (bond[0-9]+)/
92: ethbond = regex.match(%x{/sbin/ip link show #{interface}})
93: if ethbond
94: device = ethbond[1]
95: else
96: device = nil
97: end
98: device
99: end
# File lib/facter/util/ip.rb, line 102
102: def self.get_interface_value(interface, label)
103: tmp1 = []
104:
105: kernel = Facter.value(:kernel).downcase.to_sym
106:
107: # If it's not directly in the map or aliased in the map, then we don't know how to deal with it.
108: unless map = REGEX_MAP[kernel] || REGEX_MAP.values.find { |tmp| tmp[:aliases] and tmp[:aliases].include?(kernel) }
109: return []
110: end
111:
112: # Pull the correct regex out of the map.
113: regex = map[label.to_sym]
114:
115: # Linux changes the MAC address reported via ifconfig when an ethernet interface
116: # becomes a slave of a bonding device to the master MAC address.
117: # We have to dig a bit to get the original/real MAC address of the interface.
118: bonddev = get_bonding_master(interface)
119: if label == 'macaddress' and bonddev
120: bondinfo = IO.readlines("/proc/net/bonding/#{bonddev}")
121: hwaddrre = /^Slave Interface: #{interface}\n[^\n].+?\nPermanent HW addr: (([0-9a-fA-F]{2}:?)*)$/m
122: value = hwaddrre.match(bondinfo.to_s)[1].upcase
123: else
124: output_int = get_single_interface_output(interface)
125:
126: if interface != /^lo[0:]?\d?/
127: output_int.each do |s|
128: if s =~ regex
129: value = $1
130: if label == 'netmask' && convert_from_hex?(kernel)
131: value = value.scan(/../).collect do |byte| byte.to_i(16) end.join('.')
132: end
133: tmp1.push(value)
134: end
135: end
136: end
137:
138: if tmp1
139: value = tmp1.shift
140: end
141: end
142: end
# File lib/facter/util/ip.rb, line 47
47: def self.get_interfaces
48: int = nil
49:
50: output = Facter::Util::IP.get_all_interface_output()
51:
52: # We get lots of warnings on platforms that don't get an output
53: # made.
54: if output
55: int = output.scan(/^\w+[.:]?\d+/)
56: else
57: []
58: end
59: end
# File lib/facter/util/ip.rb, line 144
144: def self.get_network_value(interface)
145: require 'ipaddr'
146:
147: ipaddress = get_interface_value(interface, "ipaddress")
148: netmask = get_interface_value(interface, "netmask")
149:
150: if ipaddress && netmask
151: ip = IPAddr.new(ipaddress, Socket::AF_INET)
152: subnet = IPAddr.new(netmask, Socket::AF_INET)
153: network = ip.mask(subnet.to_s).to_s
154: end
155: end
# File lib/facter/util/ip.rb, line 71
71: def self.get_single_interface_output(interface)
72: output = ""
73: case Facter.value(:kernel)
74: when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin'
75: output = %x{/sbin/ifconfig #{interface}}
76: when 'SunOS'
77: output = %x{/usr/sbin/ifconfig #{interface}}
78: end
79: output
80: end