python 获取虚拟机磁盘和对应的标识符

import libvirt
from xml.etree import  ElementTree
#auth=[[libvirt.VIR_CRED_AUTHNAME,libvirt.VIR_CRED_NOECHOPROMPT],'admin','kvm']
USER='kvm'
PASSWD='kvm'
def authcb(credentials,user_data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = USER
        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
            credential[4] = PASSWD
    return 0
auth = [[libvirt.VIR_CRED_AUTHNAME,libvirt.VIR_CRED_PASSPHRASE],authcb ,None]
cmds="qemu+tcp://192.168.60.250/system"
try :
    #conn = libvirt.openAuth('qemu+tcp://192.168.1.230/system',auth,0)
    conn= libvirt.open(cmds)
except Exception,e:
    if "authentication failed" in  e.get_error_message():
        conn=libvirt.openAuth(cmds, auth, 0)
        print "1"
    elif "unable to connect to server" in e.get_error_message():
        print "network faild"
try :
    dom0=conn.lookupByName("TAE-Manager")
except Exception,e:
    print e
tree=ElementTree.fromstring(dom0.XMLDesc())
device= tree.findall('devices/disk/target')
block=[]
img=tree.findall('devices/disk/source')
disk=[]
for d in device:
    devices=d.get('dev')
    block.append(devices)

for i in img:
    imgs=i.get('file')
    disk.append(imgs)
x=dict(zip(block,disk))
print x

  

posted @ 2018-06-22 10:16  不会游泳的鱼pdj  阅读(280)  评论(0编辑  收藏  举报