Home | History | Annotate | Download | only in misc
      1 #!/bin/sh
      2 #
      3 # usage: rx11vnc [-s] <host>:<xdisplay>
      4 #        rx11vnc [-s] <host>            (assumes xdisplay is 0)
      5 #
      6 # -s means use ssh instead of rsh.
      7 # -S tries to tunnel the vnc traffic thru ssh.  (experimental...)
      8 #
      9 #set -xv
     10 
     11 #
     12 # Place your x11vnc cmd + options here (must have -bg and -display
     13 # with -display as the last one)
     14 #
     15 cmd="x11vnc -nap -q -bg -display"
     16 viewer="vncviewer"
     17 rsh=rsh
     18 
     19 #
     20 # The following two settings are only used under -S (ssh tunnel)
     21 #
     22 # Unfortunately, we have to set up the ssh port redirection *before*
     23 # x11vnc has started and selected its listening port.
     24 # tunnel_ports is a list of ports we expect/hope to be free on both
     25 # the local and remote machines:
     26 #
     27 tunnel_ports="5900 5901 5902 5903"
     28 #
     29 # VNC has a poor default in that if the client appears to be emanating
     30 # from the local machine, then raw encoding is preferred.  With ssh port
     31 # redirection we appear to be coming from the localhost, but we are not.
     32 # We pass this encoding list to the viewer to give lowest preference to
     33 # raw encoding:
     34 #
     35 tunnel_encodings="copyrect tight zrle hextile zlib corre rre"
     36 
     37 if [ "$USER" = "runge" ]; then
     38 	cmd="x11vnc.expt -nap -q -bg -rfbauth .vnc/passwd -display"
     39 	viewer="vncviewerz"
     40 fi
     41 
     42 if [ "X$1" = "X-s" ]; then
     43 	shift
     44 	rsh=ssh
     45 elif [ "X$1" = "X-S" ]; then
     46 	shift
     47 	rsh=ssh
     48 	tunnel=1
     49 	cmd=`echo "$cmd" | sed -e 's/ / -localhost /'`
     50 fi
     51 
     52 remote=$1
     53 if echo "$remote" | grep ':' > /dev/null; then
     54 	:
     55 else
     56 	remote="$remote:0"
     57 fi
     58 
     59 host=`echo "$remote" | awk -F: '{print $1}'` 
     60 disp=`echo "$remote" | awk -F: '{print $2}'` 
     61 disp=":$disp"
     62 if [ "X$host" = "X" ]; then
     63 	echo "bad host."
     64 	exit 1
     65 fi
     66 
     67 # start the remote x11vnc:
     68 if [ $tunnel ]; then
     69 	# much more kludgy for tunnelling:
     70 	tmp=/tmp/rx11vnc.$$
     71 	redir=""
     72 	used_ports=`netstat -an | egrep '(ESTABLISHED|LISTEN) *$' \
     73 	    | sed -e 's/^[ 	]*//'   -e 's/^tcp[ 	0-9][ 	0-9]*//' \
     74 	          -e 's/[ 	].*$//' -e 's/^.*[^0-9]//' | sort -nu`
     75 	for p in $tunnel_ports
     76 	do
     77 		ok=1
     78 		for u in $used_ports
     79 		do
     80 			if [ "X$p" = "X$u" ]; then
     81 				echo "port $u is in use. skipping it"
     82 				ok=
     83 				break
     84 			fi
     85 		done
     86 		if [ $ok ]; then
     87 			redir="$redir -L $p:localhost:$p"
     88 		fi
     89 	done
     90 	#
     91 	# Have ssh put the command in the bg, then we look for PORT=
     92 	# in the tmp file.  The sleep at the end is to give us enough
     93 	# time to connect thru the port redir, otherwise ssh will exit
     94 	# before we can connect.
     95 	#
     96 	time=15
     97 	$rsh -t -f $redir $host "$cmd $disp; echo END; sleep $time" > $tmp
     98 
     99 	i=0
    100 	while [ $i -lt $time ]
    101 	do
    102 		sleep 1
    103 		if grep '^PORT=' $tmp > /dev/null; then
    104 			port=`grep '^PORT=' $tmp | sed -e 's/PORT=//'`
    105 			if [ "X$port" != "X" ]; then
    106 				break
    107 			fi
    108 		fi
    109 		i=`expr $i + 1`
    110 	done
    111 	cat $tmp
    112 	rm -f $tmp
    113 else
    114 	port=`$rsh $host "$cmd $disp" | grep '^PORT=' | sed -e 's/PORT=//'`
    115 fi
    116 
    117 echo "x11vnc port is '$port'"
    118 
    119 # now start up the viewer on this end:
    120 if echo "$port" | grep '^[0-9][0-9]*$' > /dev/null; then
    121 	if [ $port -lt 6000 -a $port -ge 5900 ]; then
    122 		# vncviewer special cases 0-99
    123 		port=`expr $port - 5900` 
    124 	fi
    125 	if [ $tunnel ]; then
    126 		$viewer -encodings "$tunnel_encodings" "localhost:$port"
    127 	else
    128 		$viewer "$host:$port"
    129 	fi
    130 else
    131 	echo "bad port."
    132 	exit 1
    133 fi
    134