~ubuntu-branches/ubuntu/precise/facter/precise

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2011-10-18 10:32:42 UTC
  • mfrom: (1.1.10 upstream) (3.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111018103242-vvgvfa9pf057xnmj
Tags: 1.6.1-1ubuntu1
* Merge from Debian unstable (LP: #877621). Remaining changes:
  - debian/rules: use what we had in natty; we dont want ruby-pkg-tools
    in main. (LP: #408402)
* debian/control: Continue using ruby + libopenssl-ruby as Build-Depends
  even tho Debian has moved to gem2deb (not in main). Move ruby-json to
  Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
            :ipaddress  => /\s+inet (\S+)\s.*/,
28
28
            :macaddress => /(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/,
29
29
            :netmask    => /.*\s+netmask (\S+)\s.*/
 
30
        },
 
31
        :windows => {
 
32
            :ipaddress  => /\s+IP Address:\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
 
33
            :ipaddress6 => /Address ((?![fe80|::1])(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/,
 
34
            :netmask    => /\s+Subnet Prefix:\s+\S+\s+\(mask ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\)/
30
35
        }
31
36
    }
32
37
 
33
 
    # Convert an interface name into purely alpha characters.
 
38
    # Convert an interface name into purely alphanumeric characters.
34
39
    def self.alphafy(interface)
35
 
        interface.gsub(/[-:.]/, '_')
 
40
        interface.gsub(/[^a-z0-9_]/i, '_')
36
41
    end
37
42
 
38
43
    def self.convert_from_hex?(kernel)
55
60
    def self.get_interfaces
56
61
        return [] unless output = Facter::Util::IP.get_all_interface_output()
57
62
 
 
63
        # windows interface names contain spaces and are quoted and can appear multiple
 
64
        # times as ipv4 and ipv6
 
65
        return output.scan(/\s* connected\s*(\S.*)/).flatten.uniq if Facter.value(:kernel) == 'windows'
 
66
 
58
67
        # Our regex appears to be stupid, in that it leaves colons sitting
59
68
        # at the end of interfaces.  So, we have to trim those trailing
60
69
        # characters.  I tried making the regex better but supporting all
70
79
            output = %x{/usr/sbin/ifconfig -a}
71
80
        when 'HP-UX'
72
81
            output = %x{/bin/netstat -in | sed -e 1d}
 
82
        when 'windows'
 
83
          output = %x|#{ENV['SYSTEMROOT']}/system32/netsh interface ip show interface|
 
84
          output += %x|#{ENV['SYSTEMROOT']}/system32/netsh interface ipv6 show interface|
73
85
        end
74
86
        output
75
87
    end
91
103
        output
92
104
    end
93
105
 
 
106
    def self.get_output_for_interface_and_label(interface, label)
 
107
        return get_single_interface_output(interface) unless Facter.value(:kernel) == 'windows'
 
108
 
 
109
        if label == 'ipaddress6'
 
110
          output = %x|#{ENV['SYSTEMROOT']}/system32/netsh interface ipv6 show address \"#{interface}\"|
 
111
        else
 
112
          output = %x|#{ENV['SYSTEMROOT']}/system32/netsh interface ip show address \"#{interface}\"|
 
113
        end
 
114
        output
 
115
    end
 
116
 
94
117
    def self.get_bonding_master(interface)
95
118
        if Facter.value(:kernel) != 'Linux'
96
119
            return nil
139
162
            hwaddrre = /^Slave Interface: #{interface}\n[^\n].+?\nPermanent HW addr: (([0-9a-fA-F]{2}:?)*)$/m
140
163
            value = hwaddrre.match(bondinfo.to_s)[1].upcase
141
164
        else
142
 
            output_int = get_single_interface_output(interface)
 
165
            output_int = get_output_for_interface_and_label(interface, label)
143
166
 
144
167
            output_int.each_line do |s|
145
168
                if s =~ regex