如何查看环境内所有主机的已安装License并实现自动化审计采集?
VMware主机License信息自动化获取方案
问题描述
我尝试过多种获取主机License信息的方法,希望实现自动化流程,每年只需运行脚本即可将信息写入数据库用于审计,但不确定在遍历主机时如何获取License信息。
现有代码
def get_obj(si, root, vim_type): container = si.content.viewManager.CreateContainerView(root, vim_type,True) view = container.view #print(container.view) obj = [ret for ret in container.view] container.Destroy() return obj lm = content.licenseManager.licenseAssignmentManager DCS = get_obj(c, c.content.rootFolder, [vim.Datacenter]) print(DCS) print(len(DCS)) for dc in DCS: print("DATACENTEER --"+dc.name) HOSTS = get_obj(c,dc,[vim.HostSystem]) for host in HOSTS: print("HOST -- "+str(host.config.host)) print(host.hardware.systemInfo.uuid) #entity_id = host.hardware.systemInfo.uuid print(lm.QueryAssignedLicenses(str(host.config.host))) #print(host.config) #print(host.configManager) #sys.exit()
核心修正与优化
- License查询参数修正:
lm.QueryAssignedLicenses()需要传入主机的MoID(Managed Object ID),而不是host.config.host(仅为主机名/IP),正确参数为host._moId或host.id。 - 提取审计所需字段:解析返回的License对象,提取审计关键信息,示例代码如下:
for host in HOSTS: print(f"HOST -- {host.config.name}") print(f"主机UUID: {host.hardware.systemInfo.uuid}") # 使用主机MoID查询许可证 license_list = lm.QueryAssignedLicenses(host._moId) for lic in license_list: print(f"许可证密钥: {lic.licenseKey}") print(f"许可证类型: {lic.name}") print(f"过期时间: {lic.expirationDate}") print(f"总授权容量: {lic.total}") print("---")
- 数据库写入适配:将提取到的License信息封装为字典结构,通过数据库驱动(如
pymysql、sqlite3)写入目标数据库,完成自动化审计流程。
注意事项
- 确保pyVmomi版本与vCenter/ESXi服务器版本兼容。
- 运行脚本的账号需具备读取主机配置及许可证信息的权限。
内容的提问来源于stack exchange,提问作者MW_DEVEL
相关产品推荐
相关产品推荐

