Skip to content

Instantly share code, notes, and snippets.

@rilindo
Created December 11, 2011 21:00
Show Gist options
  • Save rilindo/1462717 to your computer and use it in GitHub Desktop.
Save rilindo/1462717 to your computer and use it in GitHub Desktop.
List KVM domains and their IPs. Note that the mac and IP must be in the arp table
#!/usr/bin/ruby
require 'libvirt'
require 'xml'
def maptoip(mac,vm)
IO.popen("ip neigh").each do | line|
if (line.split(/\s+/)[4] == mac)
puts "#{line.split(/\s+/)[0]} - #{mac} - #{vm}"
break
end
end
end
conn = Libvirt::open('qemu:///system')
conn.list_domains.each do |domid|
dom = conn.lookup_domain_by_id(domid)
parser = XML::Parser.string(dom.xml_desc)
doc = parser.parse
doc.find('//devices/interface').each do |s|
s.each do |node|
maptoip(node[:address],dom.name)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment