virsh show ifs

A handy script for showing the vms configured in libvirt.

While playing with virsh, I’m always interested in finding out the macs, ips, networks and vnc port of the configured virtual machines.

Thus virsh_show_ifs.


virt-addr() {
    vm_mac=$(virsh dumpxml $VM | grep "mac address" | sed "s/.*'\(.*\)'.*/\1/g")
    arp -an | grep "${vm_mac}" | awk '{ gsub(/[\(\)]/,"",$2); print $2 }'

for x in $(virsh list --all | awk '{ if (NR > 2 && $2 != "") {print $2} }') ; do \
    v=$(virsh vncdisplay $x 2> /dev/null || true)
    vip=$(virt-addr $x 2> /dev/null || true)
    echo "vm name: $x vnc: ${v:--} ip: ${vip:--}"
    virsh domiflist $x | awk '{ if (NR > 2) {print $0} }'

Here is an example output from using that script in my system

$ virsh_show_ifs
vm name: foo vnc: ip:
vnet0      bridge     virbr1     virtio      52:54:00:f2:23:49

vm name: vm1 vnc: ip:
vnet1      network    default    virtio      52:54:00:8e:f0:5a

vm name: vm2 vnc: ip:
vnet2      network    default    virtio      52:54:00:0a:dd:bc

vm name: vm3 vnc: ip:
vnet3      network    default    virtio      52:54:00:ef:4f:4d

If this is useful to you, put it in your path. And feel free to share it with your geek friends. :)