vSphere中保留了哪些虚拟交换机? (通过pyVmomi)

时间:2014-06-05 19:37:44

标签: python vmware vsphere

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

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在哪里保留这些交换机?

3 个答案:

答案 0 :(得分:4)

要使用 pyVmomi 检索 vSwitches ,您可以执行以下操作:

objDatacollectionview = CollectionViewSource.GetDefaultView(DataSource.DataGridSrcCollection)                                                                          
// Initially get all the DataRowview ItemArray , Here i get all the rows itemarray text as it is
    var temp = (from s in objDatacollectionview.OfType<DataRowView>()
                select s.Row.ItemArray).ToList();   

     for (int i = 0; i < temp.Count(); i++)
    {

   // Now in each item array , i search for the string and will get the list of the strings that match the searchText 
        var cellitems = temp[i].Where(x => x.ToString().Contains(searchText, StringComparison.OrdinalIgnoreCase)).
            Select(x => x).ToList();

      //This is to point to the row where there is first occurance of the searched text.This will execute only for the first time
      if (cellitems.Count() != 0 && !IsfirstSelectedRowIndex)
      {
        DataRowView dv = objDatacollectionview.OfType<DataRowView>().ElementAt(i);

        objDatacollectionview.MoveCurrentTo(dv);

        int cellIndex = temp[i].Select((Value, Index) => new { Value, Index }).Single(p => p.Value.Equals(cellitems[0])).Index;
        DataGridCellInfo cellinfo = new DataGridCellInfo(cellitems[0], batchDataGrid.Columns[cellIndex]);
        batchDataGrid.CurrentCell = cellinfo;

        IsfirstSelectedRowIndex = true;
      } 
}  `

结果将是:

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)

要检索分布式vSwitches ,您可以将 _get_vim_objects 功能(上图)与vim_type = vim.dvs.VmwareDistributedVirtualSwitch 参数一起使用。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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