Created
April 27, 2015 10:29
-
-
Save jriguera/e312ef13402be5f5c8bb to your computer and use it in GitHub Desktop.
Connect to a linux box to see the remote screen using VNC through ssh tunnel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Script to connect to remote machine via ssh and export X11 via vnc using a ssh tunnel | |
# | |
# Author: Jose Riguera <[email protected]> December 2009 | |
# License: GNU General Public License v2 or later | |
# | |
# x11vnc must be installed on remote host | |
# xvnc4viewer must be installed on local host | |
# | |
### Config options | |
SSHOPTIONS="-oConnectTimeout=3 -f" | |
VNCDISPLAY=":0" | |
VNCBASEPORT=5900 | |
VNCCOMMAND="x11vnc -safer -localhost -nopw -once -bg" | |
VNCVIEWER="xvnc4viewer -Shared -LowColourLevel 2" | |
### | |
PROGRAM=$(basename "$0") | |
PROGRAM_DIR=$(cd $(dirname "$0"); pwd) | |
PROGRAM_FULLPATH="$PROGRAM_DIR/$PROGRAM" | |
PROGRAM_CONF="${PROGRAM_DIR}/${PROGRAM%%.*}.conf" | |
PROGRAM_LOG="/tmp/${PROGRAM%%.*}-$(date +%F-%H-%M).log" | |
LOG="/dev/null" | |
### | |
# Avoid be interrupted by child or keyboard | |
trap "echo" SIGINT SIGSEGV SIGQUIT | |
set +e | |
# All variables have to be defined! | |
# set -o nounset | |
# Print a message and exit with error | |
die() { | |
logger -i -t "$PROGRAM" -p local0.notice -- "ERROR: $@" | |
echo "$PROGRAM ERROR: $@" | |
exit 1 | |
} | |
# Logs a message with error | |
logg() { | |
logger -i -p local0.notice -t "$PROGRAM" -- "$@" | |
echo "$PROGRAM: $@" >>${LOG} | |
} | |
# Logs and Print a message with error | |
perror() { | |
logg $@ | |
echo "$PROGRAM: $@" | |
} | |
help() { | |
cat <<EOF | |
Usage: | |
$PROGRAM [-c <file>] [-l [<file>]] [-n] user@machine [display] | |
Launch a VNC command via ssh on remote machine. | |
-c <file> Config file. Default config file is ${PROGRAM_CONF} | |
-l [<file>] Log file that stores stdout messages. Default log is ${PROGRAM_LOG} | |
-n Don't launch xvncviewer. The conexion will be waiting 120 seconds. | |
[display] X11 display on remote machine in order to connect a VNC session. | |
Default value is ":0" and will be tunneled trough 5900 port on local | |
machine. A value of ":1" will be mapped to 5901 port and so on, like | |
the VNC protocol defines. | |
EOF | |
} | |
do_sshcmd() { | |
local cmd="$1" | |
local host="$2" | |
local sshoptions="$3" | |
local cmd_status=0 | |
local cmd_ssh="ssh $sshoptions $host $cmd" | |
#echo "$cmd_ssh" | |
logg "[ $(date +%F-%H-%M) ] -> [($host):$cmd_ssh]" | |
$cmd_ssh 1>>${LOG} 2>>${LOG} | |
cmd_status=$? | |
logg "[ $(date +%F-%H-%M) ] -> [($host):$cmd_ssh]: exit=$cmd_status" | |
return $cmd_status | |
} | |
do_process() { | |
local machine="$1" | |
local vncdisplay="$2" | |
local launchvnc=$3 | |
local status=0 | |
local vncdesktop="localhost:0" | |
local port=0 | |
local vnccommand="$VNCCOMMAND -display $vncdisplay" | |
echo "Launching VNC on remote machine ..." | |
do_sshcmd "$vnccommand" "$machine" "$SSHOPTIONS" | |
sleep 5 | |
vncdesktop=$(cat ${LOG} | grep "The VNC desktop is:" | cut -d":" -f 3 ) | |
vncport=$(echo "$VNCBASEPORT + $vncdesktop" | bc) | |
echo "The VNC desktop is: localhost:$vncdesktop" | |
echo "Setting ssh tunel on port $vncport ..." | |
do_sshcmd "sleep 120" "$machine" "$SSHOPTIONS -L $vncport:localhost:$vncport" | |
if [ $launchvnc = 1 ]; then | |
echo "Launching vncviewer localhost:$vncdesktop ... " | |
echo "Press F8 for options ..." | |
$VNCVIEWER localhost:$vncdesktop >> ${LOG} 2>&1 | |
else | |
echo "Waiting 120 seconds for connections on port $vncport (localhost:$vncdesktop) ..." | |
sleep 120 | |
fi | |
status=$? | |
echo "Closing connections ..." | |
wait | |
return $status | |
} | |
######## | |
# Main Program | |
MACHINE=NONE | |
COMPONENT=none | |
DO=0 | |
LOG2FILE=1 | |
LAUNCHVNC=1 | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-c) [ ! -z "$2" ] || die "The $1 switch requires the name of a config file!" | |
arg_confile="$2" | |
shift | |
;; | |
-l) if [ ! -z "$2" ]; then | |
if echo "$2" | grep -q "\." ; then | |
arg_log="$2" | |
shift | |
fi | |
fi | |
LOG2FILE=1 | |
;; | |
-n) LAUNCHVNC=0 | |
;; | |
-h) help; exit 0 | |
;; | |
--) shift; break | |
;; | |
-*) echo "$PROGRAM: Unknown argument $1" | |
help; exit 0 | |
;; | |
*@*) | |
MACHINE="$1" | |
if [ ! -z "$2" ]; then | |
COMPONENT="$2" | |
shift | |
fi | |
DO=1 | |
;; | |
esac | |
shift | |
done | |
# [ $DO = 0 ] && help && exit 0 | |
# Configuration file | |
PROGRAM_CONF=${arg_confile:-$PROGRAM_CONF} | |
[ -f "$PROGRAM_CONF" ] && . "$PROGRAM_CONF" | |
# Logs | |
if [ $LOG2FILE = 1 ]; then | |
PROGRAM_LOG=${arg_log:-$PROGRAM_LOG} | |
echo "--- $PROGRAM_FULLPATH $PROGRAM_OPTS (at $(date))" >> $PROGRAM_LOG | |
LOG=$PROGRAM_LOG | |
fi | |
# Settings | |
[ "X$COMPONENT" != "Xnone" ] && VNCDISPLAY="$COMPONENT" | |
# Main function | |
do_process "$MACHINE" "$VNCDISPLAY" $LAUNCHVNC | |
rvalue=$? | |
exit $rvalue | |
#EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment