133 lines
3.7 KiB
Plaintext
Raw Normal View History

2024-04-13 16:57:22 -06:00
#!/bin/zsh
use bastion --group remote
2024-04-13 16:57:22 -06:00
use config --group remote
DEPENDENCIES+=(timeout ssh)
#####################################################################
USAGE__options='
-n, --name session name to test
-s, --connection-string explicit session host / ssh connection string to test
-t, --maximum-timeout maximum connection timeout before failure (in seconds)
-c, --command testing command; performs echo by default
--use-bastion true / false override for bastion preference
(default: session.name.bastion.preferred or "false")
2024-04-13 16:57:22 -06:00
'
USAGE__description='
Tests whether you can connect to a particular session or
host string.
'
#####################################################################
MAIN() {
local CONNECTION_STRING REMOTE_NAME
local TIMEOUT_SECONDS=3
local COMMAND='echo OK &>/dev/null'
local USE_BASTION
2024-04-13 16:57:22 -06:00
while [[ $# -gt 0 ]]
do
local _S=1
2024-04-13 16:57:22 -06:00
case $1 in
-n | --name )
((_S+=1))
2024-04-13 16:57:22 -06:00
REMOTE_NAME=$2
2025-02-19 21:58:02 -07:00
CONNECTION_STRING=$(remote.config.get-connection-string $REMOTE_NAME)
2024-04-13 16:57:22 -06:00
;;
2025-02-19 21:58:02 -07:00
-s | --connection_string )
((_S+=1))
2024-04-13 16:57:22 -06:00
CONNECTION_STRING="$2"
;;
-t | --maximum-timout )
((_S+=1))
2024-04-13 16:57:22 -06:00
TIMEOUT_SECONDS=$2
[[ $TIMEOUT_SECONDS -gt 0 ]] \
2025-02-19 21:58:02 -07:00
|| echo.error "invalid timeout seconds '$TIMEOUT_SECONDS'"
2024-04-13 16:57:22 -06:00
;;
-c | --command )
((_S+=1))
2024-04-13 16:57:22 -06:00
COMMAND=$2
;;
--use-bastion )
((_S+=1))
USE_BASTION=$2
case $USE_BASTION in
true | false ) ;;
2025-02-19 21:58:02 -07:00
* ) echo.error "invalid setting for '--use-bastion' (must be 'true' or 'false')" ;;
esac
2024-04-13 16:57:22 -06:00
;;
2025-02-19 21:58:02 -07:00
* ) echo.error "unrecognized argument '$1'" ;;
2024-04-13 16:57:22 -06:00
esac
2024-08-14 19:07:00 -06:00
[[ $_S -le $# ]] \
&& shift $_S \
2025-02-19 21:58:02 -07:00
|| echo.error "missing argument for '$1'" \
2024-08-14 19:07:00 -06:00
|| shift $#
2024-04-13 16:57:22 -06:00
done
[ $CONNECTION_STRING ] \
2025-02-19 21:58:02 -07:00
|| echo.error "unable to determine connection string"
2024-04-13 16:57:22 -06:00
[ $USE_BASTION ] || {
USE_BASTION=$(\
2025-02-19 21:58:02 -07:00
remote.config.query-connection-with-fallback \
".sessions.$REMOTE_NAME.bastion.preferred" \
'false' \
;
)
}
2025-02-19 21:58:02 -07:00
utils.check-errors --fail
2024-04-13 16:57:22 -06:00
2024-04-13 16:57:22 -06:00
##########################################
local BASTION_HOST
[[ $USE_BASTION =~ true ]] && {
BASTION_HOST=$(\
2025-02-19 21:58:02 -07:00
remote.config.query-connection-with-fallback \
".sessions.$REMOTE_NAME.bastion.session" \
)
}
[[ $USE_BASTION =~ true ]] && {
[ $BASTION_HOST ] \
2025-02-19 21:58:02 -07:00
|| echo.error "cannot connect to $REMOTE_NAME; no configured bastion host" \
|| return 1
}
2025-02-19 21:58:02 -07:00
2024-04-13 16:57:22 -06:00
case $CONNECTION_STRING in
localhost | $USER@localhost )
CONNECTION_TEST() { return 0; } # current user on local machine can always connect
;;
2025-02-19 21:58:02 -07:00
* )
[[ $USE_BASTION =~ true ]] && {
2025-02-19 21:58:02 -07:00
echo.debug "MAIN -n $BASTION_HOST -c \"$(remote.bastion.get-passthrough-prefix) remote test -- -n $REMOTE_NAME -c \"$COMMAND\"\""
BASTION_TARGET="$REMOTE_NAME" MAIN -n $BASTION_HOST -c "$(remote.bastion.get-passthrough-prefix) remote test -- -n $REMOTE_NAME -c \"$COMMAND\""
return $?
}
2024-04-13 16:57:22 -06:00
CONNECTION_TEST() {
[ $REMOTE_NAME ] && {
2025-02-19 21:58:02 -07:00
[[ $(remote.config.query-connection .sessions.$REMOTE_NAME.enabled) =~ false ]] && {
2024-04-13 16:57:22 -06:00
return 1
}
}
local REMOTE_ARGS=()
2025-02-19 21:58:02 -07:00
REMOTE_ARGS+=($(remote.config.get-ssh-args --type ssh $REMOTE_NAME))
2024-04-13 16:57:22 -06:00
REMOTE_ARGS+=(-o BatchMode=yes)
2025-02-19 21:58:02 -07:00
echo.debug "attempting\ntimeout $TIMEOUT_SECONDS ssh $REMOTE_ARGS $CONNECTION_STRING "'\'"\"$COMMAND"'\'"\"" >&2
timeout --foreground $TIMEOUT_SECONDS ssh ${REMOTE_ARGS[@]} "$CONNECTION_STRING" "$COMMAND" >&2
2024-04-13 16:57:22 -06:00
}
;;
esac
[ $REMOTE_NAME ] || REMOTE_NAME=explicit
2025-02-19 21:58:02 -07:00
echo.status "testing connection $CONNECTION_STRING ($REMOTE_NAME$([ $BASTION_TARGET ] && echo " -> $BASTION_TARGET"))" \
2024-04-13 16:57:22 -06:00
&& CONNECTION_TEST \
2025-02-19 21:58:02 -07:00
&& echo.success "successfully connected to '$CONNECTION_STRING' ($REMOTE_NAME)" \
|| echo.error "connection to '$CONNECTION_STRING ($REMOTE_NAME)' failed" \
2024-04-13 16:57:22 -06:00
}