Class: Facter::Util::IP::Windows

### Home Classes Methods

### In Files

- lib/facter/util/ip/windows.rb

### Parent

Object

### Namespace

- CLASS Facter::Util::IP::Windows::Bindings4
- CLASS Facter::Util::IP::Windows::Bindings6

### Methods

- ::convert_netmask_from_hex?
- ::get_preferred_ipv4_adapters
- ::get_preferred_ipv6_adapters
- ::get_preferred_network_adapters
- ::interfaces
- ::network_adapter_configurations
- ::to_s
- ::valid_ipv4_address?
- ::valid_ipv6_address?
- ::value_for_interface_and_label

### Files

- LICENSE
- Rakefile
- THANKS

### Class/Module Index

Quicksearch

- Facter
- Facter::Application
- Facter::Manufacturer
- Facter::Memory
- Facter::NetMask
- Facter::Util
- Facter::Util::CFPropertyList
- Facter::Util::CFPropertyList::CFArray
- Facter::Util::CFPropertyList::CFBoolean
- Facter::Util::CFPropertyList::CFData
- Facter::Util::CFPropertyList::CFDate
- Facter::Util::CFPropertyList::CFDictionary
- Facter::Util::CFPropertyList::CFInteger
- Facter::Util::CFPropertyList::CFReal
- Facter::Util::CFPropertyList::CFString
- Facter::Util::CFPropertyList::CFType
- Facter::Util::CFPropertyList::XML
- Facter::Util::Collection
- Facter::Util::CompositeLoader
- Facter::Util::Config
- Facter::Util::Confine
- Facter::Util::DirectoryLoader
- Facter::Util::DirectoryLoader::NoSuchDirectoryError
- Facter::Util::EC2
- Facter::Util::Fact
- Facter::Util::FileRead
- Facter::Util::IP
- Facter::Util::IP::Windows
- Facter::Util::IP::Windows::Bindings4
- Facter::Util::IP::Windows::Bindings6
- Facter::Util::Loader
- Facter::Util::Macaddress
- Facter::Util::Macaddress::Darwin
- Facter::Util::Macaddress::Windows
- Facter::Util::Macosx
- Facter::Util::MonkeyPatches
- Facter::Util::MonkeyPatches::Lines
- Facter::Util::NothingLoader
- Facter::Util::Parser
- Facter::Util::Parser::Base
- Facter::Util::Parser::JsonParser
- Facter::Util::Parser::KeyValuePairOutputFormat
- Facter::Util::Parser::NothingParser
- Facter::Util::Parser::PowershellParser
- Facter::Util::Parser::ScriptParser
- Facter::Util::Parser::TextParser
- Facter::Util::Parser::YamlParser
- Facter::Util::Processor
- Facter::Util::Registry
- Facter::Util::Resolution
- Facter::Util::SolarisZones
- Facter::Util::Uptime
- Facter::Util::Values
- Facter::Util::Virtual
- Facter::Util::Vlans
- Facter::Util::WMI
- Facter::Util::Xendomains
- Plist
- Plist::Emit
- Plist::Listener
- Plist::PArray
- Plist::PData
- Plist::PDate
- Plist::PDict
- Plist::PFalse
- Plist::PInteger
- Plist::PKey
- Plist::PList
- Plist::PReal
- Plist::PString
- Plist::PTag
- Plist::PTrue
- Plist::StreamParser
- CFFormatError
- CFPlistError
- CFTypeError
- IO
- Object
- String

No matching classes.

# Facter::Util::IP::Windows

### Constants

WINDOWS_LABEL_WMI_MAP

  Mapping fact names to WMI properties of the Win32_NetworkAdapterConfiguration

  @api private

WMI_IP_INFO_QUERY

  The WMI query used to return ip information

  @return [String]

  @api private

### Public Class Methods

convert_netmask_from_hex?() click to toggle source

Windows doesn't display netmask in hex.

@return [Boolean] false by default

@api private

    # File lib/facter/util/ip/windows.rb, line 32
    def self.convert_netmask_from_hex?
      false
    end

get_preferred_ipv4_adapters() click to toggle source

Gets a list of active IPv4 network adapter configurations sorted by the lowest IP connection metric. If two configurations have the same metric, then the IPv4 specific binding order as specified in the registry will be used.

@return [Array<WIN32OLE>]

@api private

    # File lib/facter/util/ip/windows.rb, line 113
    def self.get_preferred_ipv4_adapters
      get_preferred_network_adapters(Bindings4.new)
    end

get_preferred_ipv6_adapters() click to toggle source

Gets a list of active IPv6 network adapter configurations sorted by the lowest IP connection metric. If two configurations have the same metric, then the IPv6 specific binding order as specified in the registry will be used.

@return [Array<WIN32OLE>]

@api private

    # File lib/facter/util/ip/windows.rb, line 125
    def self.get_preferred_ipv6_adapters
      get_preferred_network_adapters(Bindings6.new)
    end

get_preferred_network_adapters(bindings) click to toggle source

Gets a list of active network adapter configurations sorted by the lowest IP connection metric. If two configurations have the same metric, then the adapter binding order as specified in the registry will be used. Note the order may different for IPv4 vs IPv6 addresses.

@see support.microsoft.com/kb/894564 @return [Array<WIN32OLE>]

@api private

    # File lib/facter/util/ip/windows.rb, line 138
    def self.get_preferred_network_adapters(bindings)
      network_adapter_configurations.select do |nic|
        bindings.bindings.include?(nic.SettingID)
      end.sort do |nic_left,nic_right|
        cmp = nic_left.IPConnectionMetric <=> nic_right.IPConnectionMetric
        if cmp == 0
          bindings.bindings[nic_left.SettingID] <=> bindings.bindings[nic_right.SettingID]
        else
          cmp
        end
      end
    end

interfaces() click to toggle source

Retrieves a list of unique interfaces names.

@return [Array<String>]

@api private

    # File lib/facter/util/ip/windows.rb, line 41
    def self.interfaces
      interface_names = []

      network_adapter_configurations.map do |nic|
        Facter::Util::WMI.execquery("SELECT * FROM Win32_NetworkAdapter WHERE Index = #{nic.Index}").each do |nic|
          interface_names << nic.NetConnectionId
        end
      end

      interface_names.uniq
    end

network_adapter_configurations() click to toggle source

Returns an array of partial Win32_NetworkAdapterConfiguration objects.

@return [Array<WIN32OLE>] objects

@api private

    # File lib/facter/util/ip/windows.rb, line 96
    def self.network_adapter_configurations
      nics = []
      # WIN32OLE doesn't implement Enumerable
      Facter::Util::WMI.execquery(WMI_IP_INFO_QUERY).each do |nic|
        nics << nic
      end
      nics
    end

to_s() click to toggle source

    # File lib/facter/util/ip/windows.rb, line 23
    def self.to_s
      'windows'
    end

valid_ipv4_address?(ip_address) click to toggle source

Determines if the value passed in is a valid ipv4 address.

@param [String] ip_address the IPv4 address to validate @return [Boolean]

@api private

    # File lib/facter/util/ip/windows.rb, line 185
    def self.valid_ipv4_address?(ip_address)
      String(ip_address).scan(/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/).each do |match|
        # excluding 169.254.x.x in Windows - this is the DHCP APIPA
        #  meaning that if the node cannot get an ip address from the dhcp server,
        #  it auto-assigns a private ip address
        unless match == "127.0.0.1" or match =~ /^169.254.*/
          return !!match
        end
      end

      false
    end

valid_ipv6_address?(ip_address) click to toggle source

Determines if the value passed in is a valid ipv6 address.

@param [String] ip_address the IPv6 address to validate @return [Boolean]

@api private

    # File lib/facter/util/ip/windows.rb, line 204
    def self.valid_ipv6_address?(ip_address)
      String(ip_address).scan(/(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4}/).each do |match|
        unless match =~ /fe80.*/ or match == "::1"
          return !!match
        end
      end

      false
    end

value_for_interface_and_label(interface, label) click to toggle source

Get the value of an interface and label. For example, you may want to find the MTU for eth0.

@param [String] interface the name of the interface returned by the {interfaces} method. @param [String] label the type of value to return, e.g. ipaddress @return [String] the value, or nil if not defined

@api private

    # File lib/facter/util/ip/windows.rb, line 61
    def self.value_for_interface_and_label(interface, label)
      wmi_value = WINDOWS_LABEL_WMI_MAP[label.downcase.to_sym]
      label_value = nil
      Facter::Util::WMI.execquery("SELECT Index FROM Win32_NetworkAdapter WHERE NetConnectionID = '#{interface}'").each do |nic|
        Facter::Util::WMI.execquery("SELECT #{wmi_value} FROM Win32_NetworkAdapterConfiguration WHERE Index = #{nic.Index}").each do |nic_config|
          case label.downcase.to_sym
          when :ipaddress
            nic_config.IPAddress.any? do |addr|
              label_value = addr if valid_ipv4_address?(addr)
              label_value
            end
          when :ipaddress6
            nic_config.IPAddress.any? do |addr|
              label_value = addr if Facter::Util::IP::Windows.valid_ipv6_address?(addr)
              label_value
            end
          when :netmask
            nic_config.IPSubnet.any? do |addr|
              label_value = addr if Facter::Util::IP::Windows.valid_ipv4_address?(addr)
              label_value
            end
          when :macaddress
            label_value = nic_config.MACAddress
          end
        end
      end

      label_value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.