Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nuriel77
Last active February 13, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuriel77/dc325084472166fd05e81baa9177ecad to your computer and use it in GitHub Desktop.
Save nuriel77/dc325084472166fd05e81baa9177ecad to your computer and use it in GitHub Desktop.
get macs libvirt for brbm network interface
#!/usr/bin/env python
import xml.etree.ElementTree as ET
import libvirt
import sys
import re
""" Find all mac addresses from domains
to be able to generate the instackenv.json
manually """
regex = r'^baremetalbrbm_[0-9]$'
def main():
conn = libvirt.openReadOnly('qemu:///system')
if conn == None:
print('Failed to open connection to qemu:///system')
exit(1)
domlist = conn.listAllDomains()
for dom in domlist:
if not re.search(regex, dom.name()):
continue
root = ET.fromstring(dom.XMLDesc())
searchString = "./devices/interface"
interface = root.find(searchString)
if interface is not None:
searchString = "./source[@network='brbm']"
source = interface.find(searchString)
if searchString is not None:
mac = interface.find('./mac').attrib['address']
print "%s: %s" % (dom.name(), mac)
else:
print "Did not find network for %s" % dom.name()
else:
print "Did not find interface entries for %s" % dom.name()
conn.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment