0
votes

I use pyVmomi to create VM on our vCenter. We have a few networks, like 'PRD-DB'. I can change the network interface of a VM to 'PRD-DB' using pyVmomi.

I know that this network address is 10.125.10.0/24. But I can't find a way of getting this network IP address using pyVmomi. What links IPs to networks ?

[EDIT] To be more precise : How can I retrieve the list of available VLANs, that I can assign to the network card of a VM ? And can I retrieve the network addresses corresponding to these VLANs ?

1

1 Answers

0
votes

Are you trying to just get the ip address for each vm?

If so you can use CreateContainerView on the vim.VirtualMachine to get the IP addresses of each VM from your vCentre via the guest object. Although you will need to have VMWare Tools installed to gather most of the information within the guest object.

serviceInstance = SmartConnect(host=host,user=user,pwd=password,port=443)
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
vm_view = content.viewManager.CreateContainerView(content.rootFolder,[vim.VirtualMachine],True)

# Loop through the vms and print the ipAddress
for vm in vm_view.view:
    print(vm.guest.ipAddress)

If you are trying to do a more advanced approach I would recommend following the getvnicinfo.py example provided on the vmware github page. This actually goes through a more detailed view getting the information for each VM. Although this doesn't seem to provide the IP Address.

To view the available vlans for a host follow the below example.

hosts = content.viewManager.CreateContainerView(content.rootFolder,[vim.HostSystem],True) 
for host in hosts.view:
    for network in host.network:
        switch = network.config.distributedVirtualSwitch
        for portgroup in switch.portgroup:
            print(portgroup.name) # This will print the vlan names