Created
February 7, 2011 10:17
-
-
Save thoughtless/814213 to your computer and use it in GitHub Desktop.
A single script for starting, connecting to, and stopping a VNC server
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
#!/bin/bash | |
# Here's how this works: | |
# 1) Open an SSH session | |
# a) Include a tunnel. Use port 5900 locally (default for VNC) and port 5901 remotely (default for tightvncserver). | |
# b) The SSH commands are run in the backgroud (so we can run other commands before they finish. | |
# c) Start tightvncserver. | |
# d) After starting tightvncserver, wait an extra 10 seconds before closing the SSH session (and the SSH tunnel). See below for when the SSH tunnel is actually closed. Note: you would need to increase this in order to have enough time to enter the password if you aren't using the passwd option to point to a password file. | |
# 2) Sleep 5 seconds. This gives enough time for the tightvncserver to start running. | |
# 3) Connect to VNC using the SSH tunnel and the password stored in /path/to/passwd. Once we do this, the SSH tunnel will be forced to stay open as long as our VNC session is open. | |
# 4) After the VNC session closes, shut off tightvncserver. | |
# Useful: http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/ | |
# Options | |
# -s Server name (hostname) or IP address | |
# -p SSH port | |
# -u User name to use for SSH tunnel | |
# -t Client-side sleep time | |
# -i Server-side sleep time. Must be greater than client-side sleep time. Defaults to double the client-side sleep time. | |
# -a Password file | |
# -g Geometry (screen resolution) | |
# -c Color depth | |
# -d Display number. This implies the port on the server side, which is always 5900 plus the display number. | |
# -o Client port | |
while getopts ":s:p:u:t:i:a:g:c:d:o:" opt; do | |
case $opt in | |
s) | |
SERVER=$OPTARG | |
;; | |
p) | |
SSH_PORT=$OPTARG | |
;; | |
u) | |
USER=$OPTARG | |
;; | |
t) | |
CLIENT_SLEEP=$OPTARG | |
;; | |
i) | |
SERVER_SLEEP=$OPTARG | |
;; | |
a) | |
PASSWORD_FILE=$OPTARG | |
;; | |
g) | |
GEOMETRY=$OPTARG | |
;; | |
c) | |
COLOR=$OPTARG | |
;; | |
d) | |
# NOTE: We can't just set the server port because doing that stops us from killing | |
# the server easily from within this script. | |
SERVER_DISPLAY=$OPTARG | |
;; | |
o) | |
CLIENT_PORT=$OPTARG | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 2 | |
;; | |
esac | |
done | |
# Set defaults | |
[ -n "$SERVER" ] || SERVER=myservername | |
[ -n "$SSH_PORT" ] || SSH_PORT=22 | |
[ -n "$USER" ] || USER=paul | |
[ -n "$CLIENT_SLEEP" ] || CLIENT_SLEEP=1 | |
[ -n "$SERVER_SLEEP" ] || SERVER_SLEEP=$(($CLIENT_SLEEP*2)) | |
if [ "$CLIENT_SLEEP" -ge "$SERVER_SLEEP" ]; then | |
echo "ERROR: Server sleep time must be greater than client sleep time." | |
echo " SERVER_SLEEP: $SERVER_SLEEP" | |
echo " CLIENT_SLEEP: $CLIENT_SLEEP" | |
exit 3 | |
fi | |
[ -n "$PASSWORD_FILE" ] || PASSWORD_FILE=/path/to/passwd | |
[ -n "$GEOMETRY" ] || GEOMETRY=1275x710 | |
[ -n "$COLOR" ] || COLOR=8 | |
[ -n "$SERVER_DISPLAY" ] || SERVER_DISPLAY=1 | |
[ -n "$SERVER_PORT" ] || SERVER_PORT=$((5900+$SERVER_DISPLAY)) | |
[ -n "$CLIENT_PORT" ] || CLIENT_PORT=5900 | |
ssh -f -L $CLIENT_PORT:localhost:$SERVER_PORT $USER@$SERVER -p $SSH_PORT "tightvncserver -nolisten tcp -localhost -nevershared -geometry $GEOMETRY -depth $COLOR :$SERVER_DISPLAY -rfbport $SERVER_PORT && sleep $SERVER_SLEEP" \ | |
&& sleep $CLIENT_SLEEP \ | |
&& vncviewer -encodings "tight" -passwd $PASSWORD_FILE localhost::$CLIENT_PORT \ | |
&& ssh $USER@$SERVER -p $SSH_PORT "tightvncserver -kill :$SERVER_DISPLAY" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment