I was working with a customer that REQUIRED all VNC connections go over an SSH Tunnel…
After Googling around a bit, I found how to do it but I wanted to my a script to automate the process in case I run into this in the future.
Here it is…
#!/bin/bash
#
#
# Matt D
# matt@oramatt.com
#
# This worked on my MBP...sooooo it should work on yours...
#
# Establish SSH Tunnel for VNC on OSX
#
#
function start_up()
{
clear screen
echo "########################################################"
echo "SSH Tunnel for VNC Viewer "
echo "########################################################"
echo
echo
echo
echo "################################################"
echo "# #"
echo "# What would you like to do ? #"
echo "# #"
echo "# 1 == Start the SSH Tunnel #"
echo "# #"
echo "# 2 == Kill the SSH Tunnel #"
echo "# #"
echo "# 3 == Do NOTHING #"
echo "# #"
echo "################################################"
echo
echo "Please enter in your choice:> "
read whatwhat
}
function start_SSHTunnel()
{
echo "What is your assigned server? "
read SERVERNAME
echo "Enter the user password when prompted..."
echo "Starting SSH Tunnel to " $SERVERNAME
ssh -L 5901:127.0.0.1:5901 -N -f -l oracle ${SERVERNAME}
# Test to make sure Real VNC vncviewer is there
cd /Applications/RealVNC/VNC\ Viewer.app/Contents/MacOS/
if [ -f vncviewer ]
then echo "Starting VNC Viewer..."
./vncviewer localhost:1 &
else echo "Real VNC Viewer Not installed..."
exit 1
fi
}
function kill_SSHTunnel()
{
echo "Attempting to kill the SSH Tunnel..."
export SSHTUNPID=`ps -ef | grep -v grep | grep 'ssh -L'| awk '{print $2}'`
kill -9 ${SSHTUNPID}
}
function do_nothing()
{
echo "################################################"
echo "So...you want to quit...yes? "
echo "Enter yes or no"
echo "################################################"
read DOWHAT
if [[ $DOWHAT = yes ]]; then
echo "Yes"
exit 1
else
echo "No"
work_time
fi
}
function work_time()
{
start_up
case $whatwhat in
1)
start_SSHTunnel
;;
2) kill_SSHTunnel
;;
3)
do_nothing
;;
esac
}
# Work time!!!
work_time
Leave a comment