dotfiles/_src.posix/private_dot_local/bin/executable_tssh-with-tunnel

48 lines
1.1 KiB
Plaintext
Raw Normal View History

2025-04-03 23:42:43 +00:00
#! /usr/bin/env bash
set -e
SCRIPT_NAME="${0##*/}"
usage() {
printf '%s\n' \
'Open a tunnel to the VMS external IP' \
'' \
'Usage:' \
" ${SCRIPT_NAME} <search> [ssh args...]" \
'' \
'Env manipulators:' \
' LOCAL_PORT default is 8443' \
' TARGET_PORT default is 443' \
' SSHUSER default is vastdata' \
' VMS default is auto-detected' \
''
exit 2
}
if [[ $# -eq 0 ]]; then
usage
fi
LOCAL_PORT="${LOCAL_PORT:-8443}"
TARGET_PORT="${TARGET_PORT:-443}"
SEARCH="$1"
SSHUSER="${SSHUSER:-vastdata}"
TARGET="${SSHUSER}@$(tsh-get "$SEARCH" "$SSHUSER")"
2025-04-04 02:47:24 +00:00
VMS="${VMS:-"$(tsh ssh "${TARGET}" -- cat /vast/vman/mgmt-vip)"}"
2025-04-03 23:42:43 +00:00
2025-04-04 02:47:24 +00:00
TUNNEL="${LOCAL_PORT}:${VMS}:${TARGET_PORT}"
2025-04-03 23:42:43 +00:00
RUN="${RUN:-}"
if [[ -z "$RUN" && -n "$(command -v zellij)" ]]; then
2025-04-04 02:47:24 +00:00
RUN="zellij run --floating --pinned 'true' --name '${TUNNEL}|${TARGET}|${SEARCH}|${SCRIPT_NAME}' --"
2025-04-03 23:42:43 +00:00
fi
2025-04-04 02:47:24 +00:00
TUNNEL="-L ${LOCAL_PORT}:${VMS}:${TARGET_PORT}"
SSH_OPTS="${SSH_OPTS:--N}"
2025-04-03 23:42:43 +00:00
printf "Executing: %s ...\n" "tsh ssh ${TUNNEL} ${TARGET} ${*:2}" >&2
2025-04-04 02:47:24 +00:00
eval "${RUN} tsh ssh ${SSH_OPTS} ${TUNNEL} '${TARGET}' ${*:2}"
2025-04-03 23:42:43 +00:00