File x11vnc_ssh of Package x11vnc

#!/bin/bash
# usage: x11vnc_ssh <host>:<xdisplay>
#  e.g.: x11vnc_ssh snoopy.peanuts.com:0

if [ -z "$1" ]; then
	echo "Usage: x11vnc_ssh [user@]host[:display] [serveropts [clientopts]]"
	echo " x11vnc_ssh does export a real(!) X11 display via VNC tunneled"
	echo " through ssh to your desktop and connects a vncviewer to it"
	echo " It assumes you have stored a password on the server side in"
	echo " ~/.vnc/passwd. (You can do that with x11vnc -storepasswd.)"
	exit 1
fi	

host=$1
disp=${host##*:}
host=${host%:*}
if [ "$host" = "$1" ]; then disp=0; fi

# You can add -threads here if it works stable enough for you
cmd="x11vnc -display :$disp -localhost -allinput -rfbauth ~/.vnc/passwd $2"
enc="copyrect tight zlib corre rre raw"

port=4

port_is_free ()
{
	while read proto rq sq local remote rest; do
		if [ ${local##*:} = $1 ]; then return 1; fi
	done < <(LANG=POSIX netstat -tan)
	return 0
}

find_free_port ()
{
	for port in `seq 4 31`; do
		if port_is_free $((port+5900)); then return; fi
	done
	echo "ERROR: No free port 5904 -- 5931 found"
	exit 1
}	

find_server_port ()
{
ssh $host "
port_is_free ()
{
        while read proto rq sq local remote rest; do
                if [ \${local##*:} = \$1 ]; then return 1; fi
        done < <(LANG=POSIX netstat -tan)
        return 0
}

find_free_port ()
{
        for port in \`seq 5900 5931\`; do
                if port_is_free \$port; then echo \$port; return; fi
        done
        echo 'ERROR: No free port 5900 -- 5931 found'
        exit 1
}
find_free_port
"
}

find_free_port
sport=`find_server_port`

echo "ssh -f -L $((port+5900)):localhost:$sport $host \"$cmd -rfbport $sport\""
ssh -f -L $((port+5900)):localhost:$sport $host "$cmd -rfbport $sport"

for i in 1 2 3
do
	sleep 2
	echo "vncviewer :$port $3"
	if vncviewer :$port $3; then break; fi
done

openSUSE Build Service is sponsored by