Python pyvmomi operation VMware (9): correctly obtain all the information of the cloned virtual machine IP

Background: The cloned virtual machine encounters a very strange phenomenon. The cloned configured IP does not match the first IP address displayed. As shown in the figure below, when I check the configured IP, the IP on the machine is also configured successfully, but it is obtained. The IP is 192.168.122.1.

Insert picture description here
Purpose: Get the IP of the cloned virtual machine object is 192.168.30.222. The
original code for obtaining the IP:

vm = self._get_obj([vim.VirtualMachine], vm_name)
if not vm:
    return {'info': {}, 'status': False}
summary = vm.summary
# IP地址
ip_address = summary.guest.ipAddress
host_name = summary.guest.hostName

It is found that this method of obtaining is extremely imprecise. When the cloned virtual machine has such a virtual IP, there is a high probability that the desired IP information cannot be obtained.
Now get the IP code:

vm = self._get_obj([vim.VirtualMachine], vm_name)
if not vm:
    return {'info': {}, 'status': False}
# IP地址
ips = []
if vm.guest.net:
    for i in vm.guest.net:
        if i.network:
            ips.extend(i.ipAddress)

The IP obtained in this way will get all the IPs of the virtual machine (including IPv6). Then
you only need to determine whether the IP you set is in the ips list.

Guess you like

Origin blog.csdn.net/qq_42631707/article/details/108659791