Created
July 8, 2013 18:58
-
-
Save axonxorz/5951474 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() { | |
echo "Usage: virt-vnc <SERVER> <VM_NAME>" | |
} | |
USER=itadmin | |
SERVER=$1 | |
VMNAME=$2 | |
if [[ -z "$SERVER" || -z "$VMNAME" ]]; then | |
usage | |
exit 1 | |
fi | |
VNCDISPLAY=`ssh ${USER}@${SERVER} "virsh -c 'qemu:///system' vncdisplay ${VMNAME} 2>/dev/null" | cut -d':' -f2 | tr -s " "` | |
if [[ "$VNCDISPLAY" == "" ]]; then | |
echo "VM $VMNAME does not exist?" | |
exit 1 | |
fi | |
# Figure out a random port to use | |
FLOOR=20000 | |
PORTNUMBER=0 #initialize | |
while [ "$PORTNUMBER" -le $FLOOR ] | |
do | |
PORTNUMBER=$RANDOM | |
done | |
VNCPORT=$(( 5900 + $VNCDISPLAY )) | |
echo "Local port is $PORTNUMBER" | |
echo "VNC Display is $VNCDISPLAY" | |
echo "VNC Port is $VNCPORT" | |
SSH=`which ssh` | |
USSH=vnc_ssh_$$ | |
TSSH=/tmp/$USSH | |
rm -f $TSSH | |
ln -s $SSH $TSSH | |
echo "$TSSH -L ${PORTNUMBER}:localhost:${VNCPORT} ${USER}@${SERVER} -Nf" | |
$TSSH -L ${PORTNUMBER}:localhost:${VNCPORT} ${USER}@${SERVER} -Nf | |
rm -f $TSSH | |
SSHPID=`pgrep $USSH` | |
echo "SSH PID is $SSHPID" | |
# I use ssvnc, some of these arguments are not appropriate for stock tightvncviewer | |
vncviewer -escape Super_L -use64 -nojpeg -x11cursor -notty -sendclipboard localhost:${PORTNUMBER} | |
kill $SSHPID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment