summaryrefslogtreecommitdiffstats
path: root/lib/facter/util/netmask.rb
blob: 5dcc576fcf537878f0c991a673ff4c16d7283fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Facter::NetMask
    def self.get_netmask
        netmask = nil;
        ipregex = %r{(\d{1,3}\.){3}\d{1,3}}

        ops = nil
        case Facter.value(:kernel)
        when 'Linux'
            ops = {
                :ifconfig => '/sbin/ifconfig',
                :regex => %r{\s+ inet\saddr: #{Facter.ipaddress} .*? Mask: (#{ipregex})}x,
                :munge => nil,
            }
        when 'SunOS'
            ops = {
                :ifconfig => '/usr/sbin/ifconfig -a',
                :regex => %r{\s+ inet \s #{Facter.ipaddress} \s netmask \s (\w{8})}x,
                :munge => Proc.new { |mask| mask.scan(/../).collect do |byte| byte.to_i(16) end.join('.') }
            }
        when 'FreeBSD','NetBSD','OpenBSD', 'Darwin', 'GNU/kFreeBSD'
            ops = {
                :ifconfig => '/sbin/ifconfig -a',
                :regex => %r{\s+ inet \s #{Facter.ipaddress} \s netmask \s 0x(\w{8})}x,
                :munge => Proc.new { |mask| mask.scan(/../).collect do |byte| byte.to_i(16) end.join('.') }
            }
        end

        %x{#{ops[:ifconfig]}}.split(/\n/).collect do |line|
            matches = line.match(ops[:regex])
            if !matches.nil?
                if ops[:munge].nil?
                    netmask = matches[1]
                else
                    netmask = ops[:munge].call(matches[1])
                end
            end
        end
        netmask
    end
end