0

我使用 pyVmomi 在我们的 vCenter 上创建 VM。我们有一些网络,例如“PRD-DB”。我可以使用 pyVmomi 将 VM 的网络接口更改为“PRD-DB”。

我知道这个网络地址是10.125.10.0/24。但我找不到使用 pyVmomi 获取此网络 IP 地址的方法。什么将 IP 链接到网络?

[编辑] 更准确地说:如何检索可用 VLAN 列表,我可以将其分配给 VM 的网卡?我可以检索与这些 VLAN 对应的网络地址吗?

4

1 回答 1

0

您是否只想获取每个虚拟机的 IP 地址?

如果是这样,您可以使用 vim.VirtualMachine 上的 CreateContainerView 通过来宾对象从您的 vCentre 获取每个 VM 的 IP 地址。尽管您需要安装 VMWare 工具来收集来宾对象中的大部分信息。

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)

如果您尝试采用更高级的方法,我建议您遵循vmware github 页面上提供的getvnicinfo.py示例。这实际上是通过更详细的视图获取每个 VM 的信息。虽然这似乎没有提供 IP 地址。

要查看主机的可用 vlan,请按照以下示例进行操作。

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
于 2018-11-07T04:56:13.690 回答