0

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 ?

Jean Coiron
  • 632
  • 8
  • 24

1 Answers1

1

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
woody1990
  • 184
  • 1
  • 12
  • Actually no, I already know how to loop through all VMs and retrieve their infos. I would like to get the list of available networks on the vCenter. For instance, when I create a VM using vMware web interface I assign a network (vlan) to the VM network card, and for this I select the vlan in a list. I want to retrieve that list from Python using pyVmomi, including the name of the vlan and the network address. So I can that say for a VM: you have this IP, so you are in vlan XXX. – Jean Coiron Nov 07 '18 at 15:50
  • I don't associate vlans to ip addresses in my environment. So I'm not sure if I can help you. I can only go as far as finding the vlan name via the distributedVirtualSwitch on each host. then loop through each portgroup within each switch to get the names I've updated my previous answer with the portgroup code example – woody1990 Nov 09 '18 at 00:17