Last active
November 10, 2015 01:17
-
-
Save shello/2ce466e15cbabde2fcbc to your computer and use it in GitHub Desktop.
Connect to VNC using TigerVNC Viewer through an SSH tunnel. OS X only.
This file contains hidden or 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 | |
# Connecto to VNC using TigerVNC Viewer through an SSH tunnel. OS X only. | |
# Requires osx_app_path: https://github.com/shello/osx_app_path | |
# Tested with bash 3.2 and zsh 5.1.1 | |
vncssh () { | |
local SSH_HOST VNC_PORT LOCAL_PORT TIGERVNC | |
local TIGERVNC_BIN="Contents/MacOS/TigerVNC Viewer" | |
SSH_HOST="$1" | |
if [[ -z "$SSH_HOST" ]]; then | |
printf "Usage: %s <ssh-host> [remote-vnc-port]\n" "$0" >&2 | |
return 1 | |
fi | |
VNC_PORT=${2:-5900} | |
if [[ ($VNC_PORT -lt 1) || ($VNC_PORT -gt 65535) ]]; then | |
printf "Invalid VNC port %s: must be an integer between 1 and 65535.\n" \ | |
"$VNC_PORT" >&2 | |
return 1 | |
fi | |
TIGERVNC="$(osx_app_path -b "com.tigervnc.tigervnc")/$TIGERVNC_BIN" | |
if [[ ! -x "$TIGERVNC" ]]; then | |
printf "TigerVNC Viewer app not found" >&2 | |
return 1 | |
fi | |
LOCAL_PORT=$((59000 + RANDOM % 1000)) | |
# "Auto-closing" tunnel | |
ssh "$SSH_HOST" -f -L${LOCAL_PORT}:localhost:"$VNC_PORT" "sleep 2" | |
"$TIGERVNC" "localhost:${LOCAL_PORT}:0.0" | |
} | |
# Run if not being sourced | |
if [[ "$BASH_SOURCE" == "$0" ]]; then | |
vncssh "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment