Class: WinNet
- Inherits:
-
Object
- Object
- WinNet
- Defined in:
- lib/vmopt/windows/win_net.rb
Defined Under Namespace
Constant Summary collapse
- WMI_IP_INFO_QUERY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The WMI query used to return ip information
WMI_IP_INFO_QUERY = ‘SELECT Description, ServiceName, IPAddress, IPConnectionMetric, InterfaceIndex, Index, IPSubnet, MACAddress, MTU, SettingID FROM Win32_NetworkAdapterConfiguration WHERE IPConnectionMetric IS NOT NULL AND IPEnabled = TRUE’
'SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPConnectionMetric IS NOT NULL AND IPEnabled = TRUE'
- WINDOWS_LABEL_WMI_MAP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Mapping fact names to WMI properties of the Win32_NetworkAdapterConfiguration
{ :ipaddress => 'IPAddress', :ipaddress6 => 'IPAddress', :macaddress => 'MACAddress', :netmask => 'IPSubnet' }
- WINDOWS_CON_STATUS_MAP =
net connect status map
{ 0 => "Disconnected", 1 => "Connecting", 2 => "Connected", 3 => "Disconnecting", 4 => "Hardware not present", 5 => "Hardware disabled", 6 => "Hardware malfunction", 7 => "Media disconnected", 8 => "Authenticating", 9 => "Authentication succeeded", 10 => "Authentication failed", 11 => "Invalid address", 12 => "Credentials required" }
Class Method Summary collapse
-
.convert_netmask_from_hex? ⇒ Boolean
private
Windows doesn’t display netmask in hex.
-
.get_preferred_ipv4_adapters ⇒ Array<WIN32OLE>
private
Gets a list of active IPv4 network adapter configurations sorted by the lowest IP connection metric.
-
.get_preferred_ipv6_adapters ⇒ Array<WIN32OLE>
private
Gets a list of active IPv6 network adapter configurations sorted by the lowest IP connection metric.
-
.get_preferred_network_adapters(bindings) ⇒ Array<WIN32OLE>
private
Gets a list of active network adapter configurations sorted by the lowest IP connection metric.
-
.interfaces ⇒ Array<String>
private
Retrieves a list of unique interfaces names.
- .netconnstatus ⇒ Object
-
.network_adapter ⇒ Array<win32ole>
private
Retrieves netadapter.
-
.network_adapter_configurations ⇒ Array<WIN32OLE>
private
Returns an array of partial Win32_NetworkAdapterConfiguration objects.
- .to_s ⇒ Object
-
.valid_ipv4_address?(ip_address) ⇒ Boolean
private
Determines if the value passed in is a valid ipv4 address.
-
.valid_ipv6_address?(ip_address) ⇒ Boolean
private
Determines if the value passed in is a valid ipv6 address.
-
.value_for_interface_and_label(interface, label) ⇒ String
private
Get the value of an interface and label.
Class Method Details
.convert_netmask_from_hex? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Windows doesn’t display netmask in hex.
63 64 65 |
# File 'lib/vmopt/windows/win_net.rb', line 63 def self.convert_netmask_from_hex? false end |
.get_preferred_ipv4_adapters ⇒ Array<WIN32OLE>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
164 165 166 |
# File 'lib/vmopt/windows/win_net.rb', line 164 def self.get_preferred_ipv4_adapters get_preferred_network_adapters(Bindings4.new) end |
.get_preferred_ipv6_adapters ⇒ Array<WIN32OLE>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
176 177 178 |
# File 'lib/vmopt/windows/win_net.rb', line 176 def self.get_preferred_ipv6_adapters get_preferred_network_adapters(Bindings6.new) end |
.get_preferred_network_adapters(bindings) ⇒ Array<WIN32OLE>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/vmopt/windows/win_net.rb', line 189 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 ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves a list of unique interfaces names.
72 73 74 75 76 77 78 79 80 |
# File 'lib/vmopt/windows/win_net.rb', line 72 def self.interfaces interface_names = [] WMI.execquery("SELECT * FROM Win32_NetworkAdapter").each do |nic| interface_names << nic.NetConnectionId unless nic.NetConnectionId.nil? or nic.NetConnectionId.empty? end interface_names.uniq end |
.netconnstatus ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/vmopt/windows/win_net.rb', line 96 def self.netconnstatus stat={} network_adapter.each do |interface| stat[interface.netConnectionId] = WINDOWS_CON_STATUS_MAP[interface.netConnectionStatus] end stat end |
.network_adapter ⇒ Array<win32ole>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves netadapter
87 88 89 90 91 92 93 94 |
# File 'lib/vmopt/windows/win_net.rb', line 87 def self.network_adapter nics = [] WMI.execquery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID IS NOT NULL" ).each do |nic| nics << nic end nics end |
.network_adapter_configurations ⇒ Array<WIN32OLE>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an array of partial Win32_NetworkAdapterConfiguration objects.
147 148 149 150 151 152 153 154 |
# File 'lib/vmopt/windows/win_net.rb', line 147 def self.network_adapter_configurations nics = [] # WIN32OLE doesn't implement Enumerable WMI.execquery(WMI_IP_INFO_QUERY).each do |nic| nics << nic end nics end |
.to_s ⇒ Object
54 55 56 |
# File 'lib/vmopt/windows/win_net.rb', line 54 def self.to_s 'windows' end |
.valid_ipv4_address?(ip_address) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if the value passed in is a valid ipv4 address.
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/vmopt/windows/win_net.rb', line 236 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) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if the value passed in is a valid ipv6 address.
255 256 257 258 259 260 261 262 263 |
# File 'lib/vmopt/windows/win_net.rb', line 255 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) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the value of an interface and label. For example, you may want to find the MTU for eth0.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/vmopt/windows/win_net.rb', line 112 def self.value_for_interface_and_label(interface, label) wmi_value = WINDOWS_LABEL_WMI_MAP[label.downcase.to_sym] label_value = nil WMI.execquery("SELECT Index FROM Win32_NetworkAdapter WHERE NetConnectionID = '#{interface}'").each do |nic| 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 IP::Windows.valid_ipv6_address?(addr) label_value end when :netmask nic_config.IPSubnet.any? do |addr| label_value = addr if IP::Windows.valid_ipv4_address?(addr) label_value end when :macaddress label_value = nic_config.MACAddress end end end label_value end |