def get_vm_network(self, vm_id): """ Get the vm's network information We only report ip info if vmware tools is running within the guest. If tools are not running we can only report back the mac address assigned by the vmx, the connected status of the device and the network attached to the device. The information for mac, networkname and connected status is available through two places, the ethernetCards backing info and through the guestInfo. Both of these codepaths are not using VimVigor and seem to be implemented in a similar manner in hostd, so they should agree with each other. Just read this from the guestInfo as well. :param vm_id: Name of the VM :rtype: VmNetworkInfo """ network_info = [] # Throws when VM is not found. vm = self.vim_client.get_vm(vm_id) if (vm.guest is None or not vm.guest.net): # No guest info so return the info from the config file return self.get_network_config(vm_id) guest_nic_info_list = vm.guest.net # vmomi list attrs are never None could be an empty list for guest_nic_info in guest_nic_info_list: if (guest_nic_info.macAddress is None): # No mac address no real guest info. Not possible to have mac # address not reporte but ip stack info available. continue info = VmNetworkInfo(mac_address=guest_nic_info.macAddress) # Fill in the connected status. if guest_nic_info.connected: info.is_connected = ConnectedStatus.CONNECTED else: info.is_connected = ConnectedStatus.DISCONNECTED # Fill in the network binding info if guest_nic_info.network is not None: info.network = guest_nic_info.network # See if the ip information is available. if guest_nic_info.ipConfig is not None: ip_addresses = guest_nic_info.ipConfig.ipAddress # This is an array due to ipv6 support for ip_address in ip_addresses: if (NetUtil.is_ipv4_address(ip_address.ipAddress)): ip = Ipv4Address( ip_address=ip_address.ipAddress, netmask=NetUtil.prefix_len_to_mask( ip_address.prefixLength)) info.ip_address = ip break network_info.append(info) return network_info
def get_vm_networks(self, vm_id): """ Get the vm's network information We only report ip info if vmware tools is running within the guest. If tools are not running we can only report back the mac address assigned by the vmx, the connected status of the device and the network attached to the device. The information for mac, networkname and connected status is available through two places, the ethernetCards backing info and through the guestInfo. Both of these codepaths are not using VimVigor and seem to be implemented in a similar manner in hostd, so they should agree with each other. Just read this from the guestInfo as well. """ network_info = [] vm = self.get_vm(vm_id) if vm.guest and vm.guest.net: for guest_nic_info in vm.guest.net: if guest_nic_info.macAddress is None: # No mac address no real guest info. Not possible to have mac # address not reported but ip stack info available. continue info = VmNetworkInfo(mac_address=guest_nic_info.macAddress) # Fill in the connected status. if guest_nic_info.connected: info.is_connected = ConnectedStatus.CONNECTED else: info.is_connected = ConnectedStatus.DISCONNECTED # Fill in the network binding info if guest_nic_info.network is not None: info.network = guest_nic_info.network # See if the ip information is available. if guest_nic_info.ipConfig is not None: ip_addresses = guest_nic_info.ipConfig.ipAddress for ip_address in ip_addresses: if self._is_ipv4_address(ip_address.ipAddress): netmask = self._prefix_len_to_mask( ip_address.prefixLength) info.ip_address = Ipv4Address( ip_address=ip_address.ipAddress, netmask=netmask) break network_info.append(info) elif vm.config and vm.config.hardware.device: for device in vm.config.hardware.device: if (isinstance(device, vim.vm.device.VirtualEthernetCard) and isinstance( device.backing, vim.vm.device.VirtualEthernetCard. NetworkBackingInfo)): info = VmNetworkInfo(mac_address=device.macAddress, network=device.backing.deviceName) network_info.append(info) return network_info
def get_vm_networks(self, vm_id): network_info = [] for net in self._client.GetVmNetworks(self._session, vm_id): info = VmNetworkInfo(network=net.name, mac_address=net.macAddress, is_connected=net.connected) if net.ipAddress: netmask = self._prefix_len_to_mask(net.prefixLength) info.ip_address = Ipv4Address(ip_address=net.ipAddress, netmask=netmask) network_info.append(info) return network_info
def get_vm_networks(self, vm_id): """ Get the vm's network information We only report ip info if vmware tools is running within the guest. If tools are not running we can only report back the mac address assigned by the vmx, the connected status of the device and the network attached to the device. The information for mac, networkname and connected status is available through two places, the ethernetCards backing info and through the guestInfo. Both of these codepaths are not using VimVigor and seem to be implemented in a similar manner in hostd, so they should agree with each other. Just read this from the guestInfo as well. """ network_info = [] vm = self.get_vm(vm_id) if vm.guest and vm.guest.net: for guest_nic_info in vm.guest.net: if guest_nic_info.macAddress is None: # No mac address no real guest info. Not possible to have mac # address not reported but ip stack info available. continue info = VmNetworkInfo(mac_address=guest_nic_info.macAddress) # Fill in the connected status. if guest_nic_info.connected: info.is_connected = ConnectedStatus.CONNECTED else: info.is_connected = ConnectedStatus.DISCONNECTED # Fill in the network binding info if guest_nic_info.network is not None: info.network = guest_nic_info.network # See if the ip information is available. if guest_nic_info.ipConfig is not None: ip_addresses = guest_nic_info.ipConfig.ipAddress for ip_address in ip_addresses: if self._is_ipv4_address(ip_address.ipAddress): netmask = self._prefix_len_to_mask(ip_address.prefixLength) info.ip_address = Ipv4Address(ip_address=ip_address.ipAddress, netmask=netmask) break network_info.append(info) elif vm.config and vm.config.hardware.device: for device in vm.config.hardware.device: if (isinstance(device, vim.vm.device.VirtualEthernetCard) and isinstance(device.backing, vim.vm.device.VirtualEthernetCard.NetworkBackingInfo)): info = VmNetworkInfo(mac_address=device.macAddress, network=device.backing.deviceName) network_info.append(info) return network_info
def get_vm_networks(self, vm_id): network_info = [] for net in self._client.GetVmNetworks(self._session, vm_id): info = VmNetworkInfo(mac_address=net.macAddress, is_connected=net.connected) if net.name: # only set network if it is neither None nor empty string info.network = net.name if net.ipAddress: netmask = self._prefix_len_to_mask(net.prefixLength) info.ip_address = Ipv4Address(ip_address=net.ipAddress, netmask=netmask) network_info.append(info) return network_info
def get_vm_networks(self, vm_id): network_info = [] for net in self._client.GetVmNetworks(self._session, vm_id): info = VmNetworkInfo(mac_address=net.macAddress) # Fill in the connected status. if net.connected: info.is_connected = ConnectedStatus.CONNECTED else: info.is_connected = ConnectedStatus.DISCONNECTED if net.name: # only set network if it is neither None nor empty string info.network = net.name if net.ipAddress: netmask = self._prefix_len_to_mask(net.prefixLength) info.ip_address = Ipv4Address(ip_address=net.ipAddress, netmask=netmask) network_info.append(info) return network_info