##!/usr/bin/env python
#-*- coding:utf8-*-
"""
@Item : pyhsclodu 2.1
@Author : william
@Group : DEV Group
@Date : 2013-10-13
Add Func : william
"""
import libvirt
from hscloud import log
import xml.etree.ElementTree as ET
LOG = log.get_logger(__name__)
def xmlParse(domName):
""" Parseing xml """
xmlPath = '/etc/libvirt/qemu/'
xmlFile = str(xmlPath + domName + '.xml')
try:
tree=ET.parse(xmlFile)
root = tree.getroot()
except:
print '%s.xml is not fond in %s' %(domName,xmlPath)
devices = root.find('devices')
for n in devices.find('interface'):
if n.tag == 'target':
pass
def vnetToip():
""" Getting all instances name """
try:
conn = libvirt.open(None)
except:
LOG.error('libvirt connection error')
vnet = dict()
domainsID = conn.listDomainsID()
for ids in domainsID:
dom_xml = conn.lookupByID(ids).XMLDesc(0)
doc = ET.fromstring(dom_xml)
interface = doc.findall('./devices/interface/target')
net_dev = interface[0].items()[0][1]
vnet[net_dev] = dict()
filterref = doc.findall('./devices/interface/filterref/')
for x in filterref:
if x.items()[0][1] == 'IP':
vnet[net_dev] = x.items()[1][1]
print vnet
if __name__ == "__main__":
sc = vnetToip()