Skip to content

Instantly share code, notes, and snippets.

@dcommander
Last active December 23, 2018 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcommander/9137ce58a92952b23fe4def73bb8c678 to your computer and use it in GitHub Desktop.
Save dcommander/9137ce58a92952b23fe4def73bb8c678 to your computer and use it in GitHub Desktop.
Rudimentary TurboVNC Session Manager
#!/bin/bash
#
# Copyright (C) 2016 D. R. Commander
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
# This script provides a rudimentary demonstration of how to implement
# session management with TurboVNC.
if [ "$1" = "" ]; then
echo
echo "USAGE: $0 <[user@]server> [session]"
echo
echo "session = existing session (e.g. \":1\") or \"new\" to force a new session"
echo " to be created"
echo
exit 1
fi
if [ ! -x /opt/TurboVNC/bin/vncviewer ]; then
echo ERROR: TurboVNC Viewer is not installed
exit 1
fi
FORCENEW=0
if [ "$2" = "new" ]; then
FORCENEW=1
elif [ "$2" != "" ]; then
echo Connecting to $1$2 ...
/opt/TurboVNC/bin/vncviewer $1$2 -tunnel
exit $?
fi
echo Listing TurboVNC sessions ...
SESSIONS=`ssh $1 "/opt/TurboVNC/bin/vncserver -list >/dev/null && /opt/TurboVNC/bin/vncserver -list | grep \:[0-9] | cut -f1"`
if [ $? -ne 0 ]; then
echo "ERROR: Could not list TurboVNC sessions on $1."
exit 1
fi
NSESSIONS=`echo $SESSIONS | wc -w`
if [ $NSESSIONS -eq 0 -o $FORCENEW = 1 ]; then
echo Starting a new session ...
SESSIONS=`ssh $1 "/opt/TurboVNC/bin/vncserver -localhost -autokill -nohttpd -securitytypes none 2>&1"`
if [ $? -ne 0 ]; then
echo "ERROR: Could not start TurboVNC session on $1."
exit 1
fi
SESSIONS=`echo $SESSIONS | sed s/.*display\ //g | sed s/\ Starting.*//g | cut -f2 -d:`
SESSIONS=\:$SESSIONS
elif [ $NSESSIONS -gt 1 ]; then
echo $NSESSIONS sessions available:
echo $SESSIONS
echo Defaulting to first session
SESSIONS=`echo $SESSIONS | cut -f1 -d' '`
fi
echo Connecting to $1$SESSIONS ...
/opt/TurboVNC/bin/vncviewer $1$SESSIONS -tunnel
exit $?
@dcommander
Copy link
Author

A much nicer TurboVNC Session Manager feature has been implemented in the 3.0 evolving/dev pre-release build of TurboVNC: https://turbovnc.org/DeveloperInfo/PreReleases. It uses the embedded SSH client in the Java TurboVNC Viewer. Please give it a try and let me know what breaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment