#!/bin/bash PROGNAME=${0##*/} main() { # Defaults for options SSH_OPTS= WM_DECORATIONS_HEIGHT=49 START_SERVER_FLAG=true START_VIEWER_FLAG=true DFLT_REMOTE_GEOMETRY=1920x1200 # Process options while [ $# -ge 1 ]; do case $1 in --ssh-opts=*) SSH_OPTS=${1#*=} ;; --wm-decorations-height=*) WM_DECORATIONS_HEIGHT=${1#*=} ;; --no-viewer) START_VIEWER_FLAG=false ;; --) shift; break ;; -*) usage ;; *) break ;; esac shift done # Process arguments [ $# = 2 ] || usage USER_AT_HOST=$1 VNC_DISPLAY_NO=$2 # Sanity checks and derivations [ "X$(setsid ssh $SSH_OPTS -n $USER_AT_HOST echo OK 2>&1)" = XOK ] || error "$USER_AT_HOST: no ssh access (has the local key file been added to the remote user's authorized_keys file?)" if check_x_connection; then LOCAL_GEOMETRY=$(xwininfo -root | sed -n 's/^ -geometry \(.*\)+0+0/\1/p') [[ $LOCAL_GEOMETRY =~ ([0-9]+)x([0-9]+) ]] REMOTE_GEOMETRY=${BASH_REMATCH[1]}x$((${BASH_REMATCH[2]}-$WM_DECORATIONS_HEIGHT)) else warning "can't query local display size; defaulting to $DFLT_REMOTE_GEOMETRY ..." REMOTE_GEOMETRY=$DFLT_REMOTE_GEOMETRY fi debug 10 "main: creating xstartup ..." ssh $SSH_OPTS $USER_AT_HOST "mkdir -p ~/.vnc; cat > ~/.vnc/xstartup; chmod 755 ~/.vnc/xstartup" < /tmp/$PROGNAME.$$.local-passwd debug 10 "main: copying password to remote ..." scp -q $SSH_OPTS /tmp/$PROGNAME.$$.local-passwd $USER_AT_HOST:/tmp/$PROGNAME.$$.remote-passwd debug 10 "main: running vncserver ..." ssh $SSH_OPTS $USER_AT_HOST -n "vncserver $VNC_DISPLAY_NO -geometry $REMOTE_GEOMETRY -rfbauth /tmp/$PROGNAME.$$.remote-passwd" ! $START_VIEWER_FLAG || vncviewer -passwd /tmp/$PROGNAME.$$.local-passwd ${USER_AT_HOST#*@}$VNC_DISPLAY_NO debug 10 "main: cleaning up old local password files ..." rm -f /tmp/$PROGNAME.*.local-passwd } check_x_connection() { xlsclients > /dev/null 2>&1 } usage() { { echo "Usage: $PROGNAME [@]" } >&2 exit 1 } error() { if check_x_connection; then gxmessage -geometry 400x100 -wrap "ERROR: $1" else echo "$PROGNAME: ERROR: $1" >&2 fi exit 1 } debug() { echo "$PROGNAME: DEBUG[$1]: $2" >&2; } main "$@"