User Tools

Site Tools


linux:ssh-tunnel-vnc-remmina

SSH Tunnel VNC/Remmina

This script establishes an SSH tunnel, starts x11vnc on the remote host, sets up port forwarding to an available local port and then starts remmina or the default vnc viewer to connect to the remote vnc server.

Set port, user and hostname in the shell script.

ssh-vnc.sh
#!/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
linux/ssh-tunnel-vnc-remmina.txt · Last modified: 2023/05/29 11:55 by 127.0.0.1