简体   繁体   English

vSphere 中的虚拟交换机保存在哪里? (通过 pyVmomi)

[英]Where are virtual switches kept in vSphere? (through pyVmomi)

I have a number of virtual portgroups on a virtual switch.我在虚拟交换机上有许多虚拟端口组。 When I execute当我执行

datacenters = si.RetrieveContent().rootFolder.childEntity
for datacenter in datacenters:
    hosts = datacenter.hostFolder.childEntity
    for host in hosts:
        networks = host.network
        for network in networks:
             print network.name

(si is a service instance) I get all the vlans (portgroups) on the network, but none of the switches (which the docs claim should be in the network directory). (si 是一个服务实例)我获得了网络上的所有 vlan(端口组),但没有任何交换机(文档声称应该在网络目录中)。 Given that folders also have the name attribute, any folders that I looked over should have been printed.鉴于文件夹也有 name 属性,我查看过的任何文件夹都应该被打印出来。 So where does vsphere/vcenter keep these switches?那么,vsphere/vcenter 在哪里保存这些开关呢?

To retrieve vSwitches with pyVmomi you can do:要使用pyVmomi检索vSwitch ,您可以执行以下操作:

def _get_vim_objects(content, vim_type):
    '''Get vim objects of a given type.'''
    return [item for item in content.viewManager.CreateContainerView(
        content.rootFolder, [vim_type], recursive=True
    ).view]

content = si.RetrieveContent()
for host in self._get_vim_objects(content, vim.HostSystem):
    for vswitch in host.config.network.vswitch:
        print(vswitch.name)

Result would be:结果将是:

vSwitch0
vSwitch1
vSwitch2

To retrieve Distributed vSwitches you can use the _get_vim_objects function (above) with vim_type= vim.dvs.VmwareDistributedVirtualSwitch parameter.要检索分布式 vSwitch,您可以使用带有 vim_type= vim.dvs.VmwareDistributedVirtualSwitch参数的_get_vim_objects函数(上文)。

Getting host.network will give you an array of network objects, but not the switch info.获取 host.network 将为您提供一组网络对象,但不会提供交换机信息。 To get switch info this is probably the most straightforward approach要获取交换机信息,这可能是最直接的方法

datacenters = si.RetrieveContent().rootFolder.childEntity
for datacenter in datacenters:
    networks = datacenter.networkFolder.childEntity
    for network in networks:
        print network.name

The network folder has the virtual switches as well as all the portgroups.网络文件夹包含虚拟交换机以及所有端口组。

This is what I use to find all DV switches in vCenter and check versions.这是我用来在 vCenter 中查找所有 DV 开关并检查版本的方法。

def find_all_dvs():
Q = "DVS unsupported versions"
try:
    log.info("Testing %s" % Q)
    host_ip, usr, pwd = vx.vc_ip, vx.vc_mu, vx.vc_mp  # Reusing credentials
    is_Old = False
    si = connect.SmartConnect(host=host_ip, user=usr, pwd=pwd, port=int("443"))
    datacenters = si.RetrieveContent().rootFolder.childEntity
    for datacenter in datacenters:
        networks = datacenter.networkFolder.childEntity
        for vds in networks:
            if (isinstance(vds, vim.DistributedVirtualSwitch)): # Find only DV switches
                log.debug("DVS version: %s, DVS name: %s" %(vds.summary.productInfo.version, vds.summary.name))
                if loose(vds.summary.productInfo.version) <= loose("6.0.0"):
                    is_Old = True
    if is_Old:
        log.info("vSphere 7.x unsupported DVS found.")
        return
    else:
        return

except Exception as err:
    log.error("%s error: %s" % (Q, str(err)))
    log.error("Error detail:%s", traceback.format_exc())
    return    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM