-
-
Save illucent/4462aa37bf124bee9a793e219e40993f to your computer and use it in GitHub Desktop.
Go from a dom_id to an IP address for a libvirt domain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import libvirt | |
from xml.dom import minidom | |
DNSMASQ_LEASES = "/var/lib/libvirt/dnsmasq/default.leases" | |
def dom_id_to_ip(dom_id): | |
conn = libvirt.openReadOnly('qemu:///system') | |
dom = conn.lookupByID(dom_id) | |
output = dom.XMLDesc(0) | |
xmldoc = minidom.parseString(output) | |
mac = xmldoc.getElementsByTagName('mac')[0].attributes['address'].value | |
with open(DNSMASQ_LEASES) as lease_file: | |
# example line: 1388705961 52:54:00:45:e1:c7 192.168.122.155 * * | |
for line in lease_file: | |
line_split = line.split() | |
if mac == line_split[1]: | |
return line_split[2] | |
# Didn't find it | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment