--- /dev/null
+#compdef vnc
+
+_vnc () {
+ local curcontext="$curcontext" state line ret=1 expl
+ typeset -A opt_args
+
+ _arguments -C -s \
+ '*:hosts:->hosts' && ret=0
+
+ if [[ -n "$state" ]]; then
+ if compset -P '*@'; then
+ _wanted hosts expl host _vnc_all_hosts && ret=0
+ else
+ _alternative \
+ 'hosts:: _vnc_all_hosts' \
+ 'users:: _vnc_users -qS@' && ret=0
+ fi
+ fi
+ return ret
+}
+
+_vnc_users () {
+ _tags users && _combination -s '@' my-accounts users-hosts users "$@"
+}
+
+_vnc_all_hosts () {
+ _tags hosts && _combination -s '@' my-accounts users-hosts hosts "$@"
+}
+
+_vnc "$@"
--- /dev/null
+# -*- sh -*-
+## vim:ft=zsh:foldmethod=marker
+
+local HOST VM
+
+if [ $# -lt 2 ]; then
+ echo "Usage: $0 HOST VM"
+ return
+fi
+HOST=$1
+VM=$2
+
+LPORT=$(( $RANDOM % 10000 + 5900))
+VNCDISPLAY=$(ssh $HOST "gnt-instance list --no-headers -o name,network_port | awk '/^$VM/ { print \$2 - 5900 } END { exit 1; }' || virsh -c qemu:///system vncdisplay $VM | sed s/://")
+
+ssh -N -f -L ${LPORT}:localhost:$(( $VNCDISPLAY + 5900 )) $HOST
+gvncviewer localhost:$(( $LPORT - 5900 ))