【问题标题】:Get Mac address of guest KVM with libvirt使用 libvirt 获取来宾 KVM 的 Mac 地址
【发布时间】:2018-07-28 23:26:07
【问题描述】:

我目前正在尝试获取 mac 地址,但我有点困惑。当我尝试

import libvirt  # To connect to the hypervisor


# Connect to your local hypervisor. See https://libvirt.org/uri.html
conn = libvirt.open('qemu+ssh://%s/system' % servidor.ubicacion_ip)  # Open the hypervisor in read-only mode
# conn = libvirt.open(None)  # Open the default hypervisor in read-write mode (require
if conn == None:
    raise Exception('Failed to open connection to the hypervisor')


dom = conn.lookupByName('userid_%s_servidor_%s.qcow2' % (user.id, servidor.id))

但是当我检查来宾python api commands in libvirtc api commands 似乎表明它应该有办法获取mac地址?

【问题讨论】:

    标签: python kvm libvirt


    【解决方案1】:

    好的,所以我找到了一种方法,使用 xml 解析 libvirt 输出。

    from salt._compat import StringIO as _StringIO
    from salt.exceptions import CommandExecutionError
    from xml.dom import minidom
    
    # Connect to your local hypervisor. See https://libvirt.org/uri.html
    conn = libvirt.open('qemu+ssh://%s/system' % servidor.ubicacion_ip)  # Open the hypervisor in read-only mode
    # conn = libvirt.open(None)  # Open the default hypervisor in read-write mode (require
    if conn == None:
        raise Exception('Failed to open connection to the hypervisor')
    
    this_vm = conn.lookupByName('userid_%s_servidor_%s.qcow2' % (user.id, servidor.id))
    
    macs = []
    doc = minidom.parse(_StringIO(this_vm.XMLDesc(0)))
    for node in doc.getElementsByTagName('devices'):
        i_nodes = node.getElementsByTagName('interface')
        for i_node in i_nodes:
            for v_node in i_node.getElementsByTagName('mac'):
                macs.append(v_node.getAttribute('address'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多