查看与vmware虚拟机相关的所有唯一ID

时间:2014-07-19 12:52:25

标签: virtual-machine vmware

我想查看特定于虚拟机的所有唯一ID,例如: 硬件ID,CPU ID,UUID,Mac地址等 有人可以帮我找到这些ID吗?

1 个答案:

答案 0 :(得分:1)

我可以帮你找到其中一些。对于其他一些事情你必须搜索文档。

安装pyVmomi并运行以下代码。

编辑:更改了在esx主机上运行的代码。只需通过python .py

运行它

现在知道这段代码是如何工作的。你必须学习Manged Objects。例如,我们使用Manged对象vm,此对象在doc中列出了许多属性。因此,为了检索vm的uuid,我们正在调用vm.config.uuid。关于其他细节,您必须查看VirtualMachine对象,了解您所需的所有属性。

import sys
import atexit
import time

from pyVmomi import vim, vmodl
from pyVim.connect import Disconnect
from pyVim import connect

inputs = {'esx_ip': '15.22.10.10',
          'esx_password': 'Password123',
          'esx_user': 'root',
          'vm_name': 'ubuntu',
          }


def wait_for_task(task, actionName='job', hideResult=False):
    """
     Waits and provides updates on a vSphere task
    """

    while task.info.state == vim.TaskInfo.State.running:
        time.sleep(2)

    if task.info.state == vim.TaskInfo.State.success:
        if task.info.result is not None and not hideResult:
            out = '%s completed successfully, result: %s' % (actionName, task.info.result)
            print out
        else:
            out = '%s completed successfully.' % actionName
            print out
    else:
        out = '%s did not complete successfully: %s' % (actionName, task.info.error)
        raise task.info.error
        print out

    return task.info.result


def get_obj(content, vimtype, name):
    """
     Get the vsphere object associated with a given text name
    """
    obj = None
    container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
    for c in container.view:
        if c.name == name:
            obj = c
            break
    return obj


def main():

    si = None

    try:
        print "Trying to connect ..."
        si = connect.Connect(inputs['vcenter_ip'], 443, inputs['vcenter_user'], inputs['vcenter_password'])

    except IOError, e:
        pass

    if not si:
        print "Cannot connect to specified host using specified username and password"
        sys.exit()
    print "Connected to vcenter!"
    atexit.register(Disconnect, si)

    content = si.RetrieveContent()

    # Get the VirtualMachine Object
    vm = get_obj(content, [vim.VirtualMachine], inputs['vm_name'])

    print "GuestID: ", vm.config.guestId
    print "UUID: ", vm.config.uuid
    print "Version: ", vm.config.version

    for device in vm.config.hardware.device:
        if isinstance(device, vim.vm.device.VirtualEthernetCard):
            print "MAC Address: ", device.macAddress

    #Example of changing UUID:
    new_uuid = '423ffff0-5d62-d040-248c-4538ae2c734f'
    vmconf = vim.vm.ConfigSpec()
    vmconf.uuid = new_uuid
    task = vm.ReconfigVM_Task(vmconf)
    wait_for_task(task, si)
    print "Successfully changed UUID"
    print "New UUID: ", vm.config.uuid

if __name__ == "__main__":
    main()