#!/bin/bash set -e SSH_ARGS='-p port user@hostname' PORT_LOCAL=15900 #check if any port between 15900 and 15920 is available and return the first available one PORT_LOCAL=`for (( i=0; i<20; i++)); do if ! nc -z 127.0.0.1 $(($i+15900)); then echo $(($i+15900)); break; fi; done` PORT_REMOTE_SERVER=5900 # If there is not a VNC process running, start one vnc_display="$(ssh $SSH_ARGS 'ps_text="$(ps x | grep x11vnc | grep -v grep)"; if [ "$ps_text" = "" ]; then x11vnc -nopw -bg -q -forever -no6 -shared -repeat -display :0 2>&1; else echo "$ps_text"; fi')" ssh -NTC -L $PORT_LOCAL:localhost:$PORT_REMOTE_SERVER $SSH_ARGS & SSH_CMD=`echo $!` sleep 3 if [ -f /usr/bin/remmina ] then remmina -c vnc://localhost:$PORT_LOCAL else vncviewer -CompressLevel=8 -QualityLevel=5 localhost:$PORT_LOCAL fi kill $SSH_CMD