summaryrefslogtreecommitdiffstats
path: root/lib/facter/macaddress.rb
blob: 4a12384d8161bf887ac317191e152d3214fe7bee (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'facter/util/macaddress'

Facter.add(:macaddress) do
    confine :operatingsystem => %w{Solaris Linux Fedora RedHat CentOS SuSE SLES Debian Gentoo Ubuntu OEL OVS GNU/kFreeBSD}
    setcode do
        ether = []
        output = %x{/sbin/ifconfig -a}
        output.each_line do |s|
            ether.push($1) if s =~ /(?:ether|HWaddr) (\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
        end
        ether[0]
    end
end

Facter.add(:macaddress) do
    confine :operatingsystem => %w{FreeBSD OpenBSD}
    setcode do
    ether = []
        output = %x{/sbin/ifconfig}
        output.each_line do |s|
            if s =~ /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/
                ether.push($1)
            end
        end
        ether[0]
    end
end

Facter.add(:macaddress) do
    confine :kernel => :darwin
    setcode { Facter::Util::Macaddress::Darwin.macaddress }
end

Facter.add(:macaddress) do
    confine :kernel => %w{AIX}
    setcode do
        ether = []
        ip = nil
        output = %x{/usr/sbin/ifconfig -a}
        output.each_line do |str|
            if str =~ /([a-z]+\d+): flags=/
                devname = $1
                unless devname =~ /lo0/
                    output2 = %x{/usr/bin/entstat #{devname}}
                    output2.each_line do |str2|
                        if str2 =~ /^Hardware Address: (\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
                            ether.push($1)
                        end
                    end
                end
            end
        end
        ether[0]
    end
end

Facter.add(:macaddress) do
    confine :kernel => %w(windows)
    setcode do
        require 'win32ole'
        require 'socket'

        ether = nil
        host = Socket.gethostname
        connect_string = "winmgmts://#{host}/root/cimv2"

        wmi = WIN32OLE.connect(connect_string)

        query = %Q{
          select *
          from Win32_NetworkAdapterConfiguration
          where IPEnabled = True
        }

        wmi.ExecQuery(query).each{ |nic|
          ether = nic.MacAddress
          break
        }
        
        ether
    end
end