~ubuntu-branches/ubuntu/trusty/facter/trusty

« back to all changes in this revision

Viewing changes to lib/facter/util/ip.rb

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock, Nigel Kersten, Andrew Pollock
  • Date: 2009-04-13 15:20:21 UTC
  • mfrom: (1.1.5 upstream) (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090413152021-t3uagjamr3ee4njl
Tags: 1.5.4-1
[ Nigel Kersten ]
* New upstream release
* Switch maintainer to pkg-puppet-devel list
* Modify uploaders
* Update watch file regexp to exclude release canditate tarballs
* Use upstream install.rb script to build rather than copying manually

[ Andrew Pollock ]
* debian/control: add libopenssl-ruby to build dependencies
* debian/control: bump Standards-Version (no changes)
* debian/compat: increase to 5
* debian/control: add pciutils and ${misc:Depends} to dependencies

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module Facter::IPAddress
2
 
    
 
1
# A base module for collecting IP-related
 
2
# information from all kinds of platforms.
 
3
module Facter::Util::IP
 
4
    # A map of all the different regexes that work for
 
5
    # a given platform or set of platforms.
 
6
    REGEX_MAP = {
 
7
        :linux => {
 
8
            :ipaddress => /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
 
9
            :macaddress  => /(?:ether|HWaddr)\s+(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/,
 
10
            :netmask => /Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
 
11
        },
 
12
        :bsd => {
 
13
            :aliases => [:openbsd, :netbsd, :freebsd, :darwin],
 
14
            :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
 
15
            :macaddress  => /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/,
 
16
            :netmask => /netmask\s+0x(\w{8})/
 
17
        },
 
18
        :sunos => {
 
19
            :addr => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
 
20
            :macaddress  => /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/,
 
21
            :netmask => /netmask\s+(\w{8})/
 
22
        }
 
23
    }
 
24
 
 
25
    # Convert an interface name into purely alpha characters.
 
26
    def self.alphafy(interface)
 
27
        interface.gsub(/[:.]/, '_')
 
28
    end
 
29
 
 
30
    def self.convert_from_hex?(kernel)
 
31
        kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin]
 
32
        kernels_to_convert.include?(kernel)
 
33
    end
 
34
 
 
35
    def self.supported_platforms
 
36
        REGEX_MAP.inject([]) do |result, tmp|
 
37
            key, map = tmp
 
38
            if map[:aliases]
 
39
                result += map[:aliases]
 
40
            else
 
41
                result << key
 
42
            end
 
43
            result
 
44
        end
 
45
    end
 
46
 
3
47
    def self.get_interfaces
4
 
    
5
 
     int = nil
6
 
    
7
 
     output =  Facter::IPAddress.get_all_interface_output()
8
 
 
9
 
     # We get lots of warnings on platforms that don't get an output
10
 
     # made.
11
 
     if output
12
 
         int = output.scan(/^\w+[.:]?\d+/)
13
 
     else
14
 
         []
15
 
     end
16
 
    
 
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
17
59
    end
18
60
 
19
61
    def self.get_all_interface_output
20
62
        case Facter.value(:kernel)
21
 
            when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD'
22
 
                output = %x{/sbin/ifconfig -a}
23
 
            when 'SunOS'
24
 
                output = %x{/usr/sbin/ifconfig -a}
 
63
        when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin'
 
64
            output = %x{/sbin/ifconfig -a}
 
65
        when 'SunOS'
 
66
            output = %x{/usr/sbin/ifconfig -a}
25
67
        end
26
68
        output
27
69
    end
29
71
    def self.get_single_interface_output(interface)
30
72
        output = ""
31
73
        case Facter.value(:kernel)
32
 
            when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD'
33
 
                    output = %x{/sbin/ifconfig #{interface}}
34
 
            when 'SunOS'
 
74
        when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin'
 
75
            output = %x{/sbin/ifconfig #{interface}}
 
76
        when 'SunOS'
35
77
            output = %x{/usr/sbin/ifconfig #{interface}}
36
78
        end
37
79
        output
38
80
    end
39
81
 
 
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
 
100
 
40
101
 
41
102
    def self.get_interface_value(interface, label)
42
 
    
43
 
    tmp1 = []
44
 
 
45
 
    case Facter.value(:kernel)
46
 
      when 'Linux'
47
 
       addr = /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
48
 
       mac  = /(?:ether|HWaddr)\s+(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
49
 
       mask = /Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
50
 
     when 'OpenBSD', 'NetBSD', 'FreeBSD'
51
 
       addr = /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
52
 
       mac  = /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/
53
 
       mask = /netmask\s+(\w{10})/
54
 
    when 'SunOS'
55
 
       addr = /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
56
 
       mac  = /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/
57
 
       mask = /netmask\s+(\w{8})/
58
 
    end
59
 
 
60
 
    case label
61
 
      when 'ipaddress'
62
 
       regex = addr
63
 
      when 'macaddress'
64
 
       regex = mac
65
 
      when 'netmask'
66
 
       regex = mask
67
 
    end 
68
 
 
69
 
    output_int = get_single_interface_output(interface)
70
 
     
71
 
      if interface != "lo" && interface != "lo0"
72
 
        output_int.each { |s|
73
 
           if s =~ regex
74
 
               value = $1
75
 
               if label == 'netmask' && Facter.value(:kernel) == "SunOS"
76
 
                   value = value.scan(/../).collect do |byte| byte.to_i(16) end.join('.') 
77
 
               end
78
 
               tmp1.push(value)
79
 
           end
80
 
       }
81
 
      end
82
 
 
83
 
      if tmp1 
84
 
        value = tmp1.shift
85
 
      end
86
 
 
87
 
   end
 
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
 
143
  
 
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
88
156
end