Since you're using libvirt
, you can just ask it!
root@onhost1:~# virsh list
Id Name State
----------------------------------
1 one-34 running
2 one-36 running
3 one-38 running
root@onhost1:~# virsh vncdisplay one-34
:34
root@onhost1:~# virsh vncdisplay 1
:34
(my particular correlation of name to VNC display port is due to the use of Open Nebula)
EDIT: Nowadays, you can use domdisplay to get the URI of the display whether it's VNC or Spice:
○ → virsh domdisplay win-michael
vnc://127.0.0.1:0
○ → for dom in $(virsh list --name); do echo -n "$dom: "; virsh domdisplay $dom; done
win-michael-m: vnc://127.0.0.1:0
Here's a one-liner to execute this for all running guests at once:
for i in $(virsh -q list|awk '{print $2}'|sort); do
echo -e "\033[01;31m$i\033[00m -> $(virsh vncdisplay $i)"
done
Also made it into a function that sorts output by port number:
function vnc-list
{
for i in $(virsh -q list|awk '{print $2}'|sort); do
PORTNUM=$(virsh vncdisplay $i|cut -f 2 -d ':')
printf "% 2d: \033[01;32m%.20s\033[00m\n" "$PORTNUM" "$i";
done | sort -n
}