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} \s netmask \s (\w{8})}x,
18: :munge => Proc.new { |mask| mask.scan(/../).collect do |byte| byte.to_i(16) end.join('.') }
19: }
20: when 'FreeBSD','NetBSD','OpenBSD', 'Darwin', 'GNU/kFreeBSD', 'DragonFly'
21: ops = {
22: :ifconfig => '/sbin/ifconfig -a',
23: :regex => %r{\s+ inet \s #{Facter.ipaddress} \s netmask \s 0x(\w{8})}x,
24: :munge => Proc.new { |mask| mask.scan(/../).collect do |byte| byte.to_i(16) end.join('.') }
25: }
26: end
27:
28: %x{#{ops[:ifconfig]}}.split(/\n/).collect do |line|
29: matches = line.match(ops[:regex])
30: if !matches.nil?
31: if ops[:munge].nil?
32: netmask = matches[1]
33: else
34: netmask = ops[:munge].call(matches[1])
35: end
36: end
37: end
38: netmask
39: end