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

问题描述 投票:2回答:3

我在虚拟交换机上有许多虚拟端口组。当我执行

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(端口组),但是没有任何交换机(文档声称它们应该在网络目录中)。假设文件夹也具有name属性,那么我查看过的所有文件夹都应该已打印。那么,vsphere / vcenter在哪里保留这些开关?

python vmware vsphere
3个回答
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参数一起使用。


0
投票

获取host.network将为您提供一系列网络对象,但不会提供交换机信息。要获取切换信息,这可能是最直接的方法

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

网络文件夹具有虚拟交换机以及所有端口组。


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    
© www.soinside.com 2019 - 2024. All rights reserved.