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

2 投票
3 回答
3431 浏览
提问于 2025-04-18 08:47

我在一个虚拟交换机上有几个虚拟端口组。当我执行

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是一个服务实例)时,我能看到网络上的所有VLAN(端口组),但却没有看到任何交换机(文档上说这些交换机应该在网络目录里)。因为文件夹也有名字属性,所以我查看过的任何文件夹都应该被打印出来。那么,vsphere/vcenter把这些交换机放在哪里了呢?

3 个回答

0

这是我用来在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    
0

获取 host.network 会给你一个网络对象的数组,但不会包含交换机的信息。要获取交换机的信息,这可能是最简单的方法。

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

网络文件夹里有虚拟交换机和所有的端口组。

4

要用pyVmomi来获取虚拟交换机(vSwitches),你可以这样做:

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)

得到的结果会是:

vSwitch0
vSwitch1
vSwitch2

如果你想获取分布式虚拟交换机(Distributed vSwitches),可以使用上面提到的_get_vim_objects函数,并把参数vim_type设置为vim.dvs.VmwareDistributedVirtualSwitch

撰写回答