Skip to content

Instantly share code, notes, and snippets.

@fredhsu
Created October 2, 2013 22:33
Show Gist options
  • Save fredhsu/6801488 to your computer and use it in GitHub Desktop.
Save fredhsu/6801488 to your computer and use it in GitHub Desktop.
Libvirt Python API sample, searching for a VM with a particular MAC address
import libvirt
import xml.etree.ElementTree as ET
def findDomainWithMac(conn, addr):
domlist = map(conn.lookupByID, conn.listDomainsID())
for dom in domlist:
root = ET.fromstring(dom.XMLDesc())
searchString = "./devices/interface/mac[@address='{0}']".format(addr)
if (root.find(searchString) is not None):
return dom
conn = libvirt.openReadOnly('qemu:///system')
domain = findDomainWithMac(conn, '52:54:00:b6:53:32')
print domain.name()
@nickgarvey
Copy link

See my fork, notice the addition of the "0" on line 7: https://gist.github.com/nickgarvey/8229208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment