我目前正在尝试获取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 libvirt时,c api commands似乎表明它应该有一种获取mac地址的方法?
答案 0 :(得分:0)
好吧,所以我找到了一种方法,使用libvirt输出的xml解析。
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'))