From 03eb0cad29e677f0b1e49f7eba8e581ecaaa2018 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sat, 26 Aug 2023 09:09:09 -0400 Subject: it works except the terminal is echoing when ssh is called with '-tt' it echoes back its stdin to the terminal, which dumps the source code of the rpc there it echoes before the remote bash code even runs, like how the terminal sometimes buffers input in the display when there is nothing on the other end listening for it. the solution is probably running 'ssh' from within 'socat' as documented in socat(1) --- src/rpc.bash | 63 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/rpc.bash b/src/rpc.bash index eb41538..44f7020 100644 --- a/src/rpc.bash +++ b/src/rpc.bash @@ -50,22 +50,28 @@ recursive_dependencies() # Input: $BASH_RPC_REMOTE_DEST - hostname passed to ssh (uses ssh host aliases) # Input: $BASH_RPC_SSH_OPTIONS - option arguments passed to ssh (they precede '--') -# Input: $1 - bash shell script source code to run remotely -# Input: $2, $3, ... - command line arguments passed to the remote bash shell -# Input: stdin, stdout, stderr: passed to ssh +# Input; $BASH_RPC_STDIN_IS_TTY - controls whether to allocate ssh pty. Doesn't work. +# Input: $1 - shell script source code to run in the remote bash +# Input: $2, $3, ... - command line arguments passed to the remote bash +# Input: stdin, stdout, stderr: passed to the remote bash over ssh +# Input: "$0" - copied to remote bash's $0 +# Input: "$SHELL" - remote shell to launch (better be bash) bash_rpc_remote_run_script() { - ( - local script="$1" - shift - set -- "$SHELL" -c "${script@Q}" "$0" "${@@Q}" - set -x - exec ssh \ - "${BASH_RPC_SSH_OPTIONS[@]}" \ - -- \ - "${BASH_RPC_REMOTE_DEST:-localhost}" \ - "$@" - ) + local script="$1" + shift + exec ssh \ + "${BASH_RPC_SSH_OPTIONS[@]}" -- \ + "${BASH_RPC_REMOTE_DEST:-localhost}" \ + "$SHELL" -c "${script@Q}" "$0" "${@@Q}" +} + +with_ssh_option() +{ + local BASH_RPC_SSH_OPTIONS + BASH_RPC_SSH_OPTIONS+=("$1") + shift + "$@" } # Preserves the rest of stdin for the command. @@ -73,14 +79,20 @@ bash_rpc_eval_stdin() { [ "$1" -gt 0 ] || return read -N"$1" -r || return - shift # prepare "$@" for script execution context - source <(printf '%s' "$REPLY" | tr -d '\015') + + # Prepare "$@" for script execution context. + shift + + # Even unset REPLY to leave pristine environment. + eval "unset REPLY; $(cat <<< "$REPLY")" } remote_run_function_simple() { script_source=$(declare -f "$1" && printf '"$@";\n') - bash_rpc_remote_run_script "$script_source" "$@" + bash_rpc_remote_run_script \ + "$script_source" \ + "$@" } remote_run_function() @@ -91,15 +103,16 @@ remote_run_function() declare -f $funcs printf '"$@"\n' "$main" ) - BASH_RPC_SSH_OPTIONS=(-tt) + + local BASH_RPC_STDIN_IS_TTY=$([ ! -t 0 ] || echo y) { printf '%s' "$stage2_source" - if [ -t 0 ] - then - socat stdio PTY - else - cat - fi - } | remote_run_function_simple bash_rpc_eval_stdin "${#stage2_source}" "$@" + cat + } | + ${BASH_RPC_STDIN_IS_TTY:+ with_ssh_option '-tt'} \ + remote_run_function_simple \ + bash_rpc_eval_stdin \ + "${#stage2_source}" \ + "$@" } -- cgit v1.2.3