Skip to content

Instantly share code, notes, and snippets.

@illucent
Forked from nickgarvey/gist:8229874
Created November 4, 2016 18:05
Show Gist options
  • Save illucent/4462aa37bf124bee9a793e219e40993f to your computer and use it in GitHub Desktop.
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
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