Skip to content

Instantly share code, notes, and snippets.

@princebot
Last active October 21, 2015 17:47
Show Gist options
  • Save princebot/55f048ab4717ef21504b to your computer and use it in GitHub Desktop.
Save princebot/55f048ab4717ef21504b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Actions: Hop through the [redacted] NAT box to SSH into [redacted] servers.
# Globals: None.
# Options: --key, -k, -i PRIVATE_KEY
# Use this key for authentication (default: standard SSH key
# locations [see man page ssh (1)]
# --debug, -d
# Run in debugger mode: set shell options -x and -v, and run
# ssh in verbose mode with -vvv
# Returns: 0 for success, 1 for error.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
chainsshaw() (
local prog=${FUNCNAME}
local usage=$(echo -e \
"\nUsage: ${prog} [--key PRIVATE_KEY] [--debug] [USER@]HOST\n" \
"\nHop through the [redacted] NAT box to SSH into [redacted] servers.")
if ! (($#)); then
echo -e "\n${prog}: Error: No arguments" >&2
echo "${usage}" >&2
return 1
fi
local key
local dest
local opt_debug
while (($# > 0)); do
case $1 in
-h|--help)
echo "${usage}"
return
;;
-d|--debug)
opt_debug="-vvv"
echo "================================================="
echo "Entering debug mode"
echo "================================================="
set -xv
shift 1
;;
--key|-k|-i)
if [[ ! $2 ]]; then
echo "chainsshaw(): Error: Missing argument for --key" \
"option" >&2
return 1
elif [[ ! -f $2 ]]; then
echo "chainsshaw(): Error: \"$2\" file does not exist" >&2
return 1
fi
key=$2
shift 2
;;
*)
if [[ ${dest} ]]; then
echo "chainsshaw(): Error: Too many arguments" >&2
return 1
fi
dest=$1
shift
;;
esac
done
local user=${dest%%@*}
local opt_user
if [[ ${user} ]]; then
opt_user="-l ${user}"
fi
if [[ ${key} ]]; then
ssh ${opt_debug} -i "${key}" \
-oProxyCommand="ssh ${opt_user} -W %h:%p <hostname or IP>" \
"${dest}"
else
ssh ${opt_debug} \
-oProxyCommand="ssh ${opt_user} -W %h:%p <hostname or IP>" \
"${dest}"
fi
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment