Similar to my question here, except with multiple ssh, and similar to this question but with white space in the arguments...
I would like to run a local script, test.sh
, on a remote server via multiple ssh. test.sh
takes an argument that often has multiple words.
test.sh:
#!/bin/bash
while getopts b: opt;
do
case $opt in
b)
bval="$OPTARG"
;;
esac
done
echo $bval
call_test.sh
#!/bin/bash
# Do some stuff
PHRASE="multi word arg"
ssh -A user@host1 "bash -s" -- < ./test.sh -b "${PHRASE@Q}"
and run ./call_test.sh
, this correctly outputs
multi word arg
But when I change the last line of call_test.sh
to the following:
ssh -A user@host1 ssh -A user@host2 "bash -s" -- < ./test.sh -b "${PHRASE@Q}"
and run ./call_test.sh
, it outputs:
multi
Basically, changing from single SSH to multiple SSH breaks my multi-word argument.
I think that the multiple ssh commands are unwrapping the quotes for the multi-word argument at each step, but I'm not sure how to prevent that. Any ideas how to successfully pass the multi-word argument to the script running across multiple ssh?
Edit:
I believe this answer gets at the problem that I'm running into.
ssh host1 ssh host2 ... ssh hostn
) will require an additional layer of quotes. That way lies insanity. You should instead useProxyJump
s orProxyCommand
s (see edit in the question as well as the answer), so that SSH internally manages the hops, so you only need to quote for the ssh command you actually execute.DisableForwarding yes
) How would the-J
work?DisableForwarding
is about forwarding ports or X11 or agents. Unless there's a ForceCommand blocking execution of anything else, proxyjump should work. Or, if OP can run netcat, there's also that as a worst case option.