[libvirt] Using libvirt to obtain mac address of virtual domain

Greetings, I am new to libvirt and am looking for the most efficient way to programmatically obtain the mac address from a virtual domain. Libvirt is providing access to xen and kvm in my case. A nudge in the right direction would be appreciated! Best Regards, Devin Carlen

Le lundi 19 octobre 2009 à 17:27 -0500, Carlen, Devin C. (ARC-A)[PEROT SYSTEMS] a écrit :
Greetings, I am new to libvirt and am looking for the most efficient way to programmatically obtain the mac address from a virtual domain. Libvirt is providing access to xen and kvm in my case. A nudge in the right direction would be appreciated!
Hi. If you need to get the mac address from bash, you can use this: virsh dumpxml myguest | grep 'mac address' | cut -d\' -f2 You'll get one mac address per line (one line per NIC on the guest) Regards, Daniel
Best Regards,
Devin Carlen
-- Libvir-list mailing list Libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
-- Daniel Berteaud FIREWALL-SERVICES SARL. Société de Services en Logiciels Libres Technopôle Montesquieu 33650 MARTILLAC Tel : 05 56 64 15 32 Fax : 05 56 64 15 32 Mail: daniel@firewall-services.com Web : http://www.firewall-services.com

Daniel Berteaud wrote:
If you need to get the mac address from bash, you can use this:
virsh dumpxml myguest | grep 'mac address' | cut -d\' -f2
You'll get one mac address per line (one line per NIC on the guest)
An alternate approach which doesn't depend on the specific manner in which the XML is pretty-printed follows: virsh dumpxml myguest \ | xmlstarlet sel -t \ -m "/domain/devices/interface[@type='network']/mac" \ -v '@address' \ -n This does add an external dependency (see http://xmlstar.sf.net/), but I find XMLStarlet useful in conjunction with libvirt in other cases -- for instance, for programatically adding or modifying devices within the domain XML.

On Tue, Oct 20, 2009 at 11:30:12PM -0500, Charles Duffy wrote:
Daniel Berteaud wrote:
If you need to get the mac address from bash, you can use this:
virsh dumpxml myguest | grep 'mac address' | cut -d\' -f2
You'll get one mac address per line (one line per NIC on the guest)
An alternate approach which doesn't depend on the specific manner in which the XML is pretty-printed follows:
virsh dumpxml myguest \ | xmlstarlet sel -t \ -m "/domain/devices/interface[@type='network']/mac" \ -v '@address' \ -n
This does add an external dependency (see http://xmlstar.sf.net/), but I find XMLStarlet useful in conjunction with libvirt in other cases -- for instance, for programatically adding or modifying devices within the domain XML.
Agreed, its cleaner ! I added a --xpath option to xmllint to allow virsh dumpxml myguest | xmllint --xpath "//interface[@type='network']/mac" but I commited this last week only so it's not yet in any libxml2 release. When libxml2-2.7.7 or successors are out then this may be sufficient (but less complete than XMLStarlet !) Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/
participants (4)
-
Carlen, Devin C. (ARC-A)[PEROT SYSTEMS]
-
Charles Duffy
-
Daniel Berteaud
-
Daniel Veillard