Attach to an existing X session with x11vnc over SSH
Most of the stuff I do is via the shell, but once in a while need to attach to an existing X session on my work desktop. My requirements are that the method:
- Uses SSH for authentication
- Tunnels VNC traffic over SSH for security and so no additional ports need to be opened
x11vnc is what I've been using. Here's a little shell script I wrote that will take user@hostname as a variable, enumerate an existing GDM session on the remote computer, create an SSH tunnel and attach to it via VNC. It uses aggressive compression, which looks a bit ugly but still works over a low bandwidth connection. The auth file enumeration bit works with Gnome, but could probably be adapted for use with other window managers.
#!/bin/sh
die () {
echo >&2 "$@"
exit 1
}
echo $1 | grep -E -q '[a-zA-Z0-9]\@[a-zA-Z0-9]' || die "\n Missing username and host, try:\n ./vnc.sh user@host\n"
USERHOST=$1
USER=$(echo $USERHOST | awk -F@ '{print$1}')
AUTHFILE=/tmp/$(ssh $USERHOST 'ls -la /tmp | grep $USER | grep gdm' | awk '{print$NF}')
echo ""
echo " ------------------------------------------"
echo " Attempting to use the following variables:"
echo " ------------------------------------------"
echo " USERHOST... " $USERHOST
echo " USER....... " $USER
echo " AUTHFILE... " $AUTHFILE
echo ""
sleep 3
ssh -f -L 5900:localhost:5900 $USERHOST 'x11vnc -noxdamage -display :0 -auth' $AUTHFILE && sleep 5 && vncviewer -encodings "copyrect tight zrle hextile" localhost:0
Leave a comment