2: def self.get_netmask
3: netmask = nil;
4: ipregex = %r{(\d{1,3}\.){3}\d{1,3}}
5:
6: ops = nil
7: case Facter.value(:kernel)
8: when 'Linux'
9: ops = {
10: :ifconfig => '/sbin/ifconfig',
11: :regex => %r{\s+ inet\saddr: #{Facter.ipaddress} .*? Mask: (#{ipregex})}x,
12: :munge => nil,
13: }
14: when 'SunOS'
15: ops = {
16: :ifconfig => '/usr/sbin/ifconfig -a',
17: :regex => %r{\s+ inet\s+? #{Facter.ipaddress} \+? mask (\w{8})}x,
18: :munge => Proc.new { |mask| mask.scan(/../).collect do |byte| byte.to_i(16) end.join('.') }
19: }
20: end
21:
22: %x{#{ops[:ifconfig]}}.split(/\n/).collect do |line|
23: matches = line.match(ops[:regex])
24: if !matches.nil?
25: if ops[:munge].nil?
26: netmask = matches[1]
27: else
28: netmask = ops[:munge].call(matches[1])
29: end
30: end
31: end
32: netmask
33: end