How to run a local script with whitespace in arguments on a remote machine through multiple SSH?

Jacob Stern

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.

pizdelect

If you pass it through 2 ssh commands, you have to quote-escape the string TWICE. Do that with an extra PHRASE=${PHRASE@Q} assignment.

Beware however that both ${var@Q} and printf %q (described below) will use the $'...' quote-escape format, which may not be supported by the remote shell.

After fixing the last line of your test.sh script to echo "$bval" instead of echo $bval:

PHRASE="multi word     arg"
PHRASE=${PHRASE@Q}
ssh -A user@host1 ssh -A user@host2 "bash -s" -- < ./test.sh -b "${PHRASE@Q}"

multi word     arg

Instead of the ${var@P} expansion form which is not supported in older versions of bash, you can use printf -v var %q:

PHRASE="multi word     arg"
printf -v PHRASE %q "$PHRASE"
printf -v PHRASE %q "$PHRASE"
ssh -A user@host1 ssh -A user@host2 "bash -s" -- < ./test.sh -b "$PHRASE"

Also, instead of giving your script via stdin, which is inefficient (bash will do a read system call for each byte when reading the script from stdin), you can just put the whole script and its arguments in a variable:

printf -v cmd 'bash -c %q bash -b %q' "$(cat test.sh)" 'multi word   wahterve'
printf -v cmd %q "$cmd"
ssh localhost ssh localhost "$cmd"

multi word   wahterve

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to run local bash script through multiple ssh

From Dev

Run shell script on remote machine with SSH using local environment variable

From Dev

How can I execute local script on remote machine and include arguments?

From Dev

How to run a webserver script on remote server through SSH?

From Dev

How to run local shell script on remote server via SSH?

From Dev

How to run local shell script on remote server via SSH?

From Dev

Can fabric run a local script on a remote machine?

From Dev

Run local python script on remote machine

From Dev

Shell Script to run on Local and Remote machine

From Dev

Run local script in other machine using ssh

From Dev

Run a remote script on a local file via SSH

From Dev

How to run a local script with a multiple-word argument on a remote server?

From Dev

How to pass multiple arguments through ssh and use those arguments in the ssh script?

From Dev

Run MySQL query on remote machine through ssh in command line

From Dev

How to pass multiple arguments in a command to execute an exe on remote linux machine using C/C++ and SSH

From Dev

How to execute local script by passing arguments on remote via ssh as a different user?

From Dev

How to run appium script in remote machine?

From Dev

How to run a complex awk script on a remote machine?

From Dev

how to run script on remote machine with delay

From Dev

How to run local shell script on remote GCP machine via gcloud compute?

From Dev

SSH + Sudo + Expect in Bash script: Run command with sudo in remote machine

From Dev

Run remote R process using script on local machine

From Dev

When I run the script on a remote server it starts up on the local machine

From Dev

How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer?

From Dev

How to execute a shell script stored on remote machine from a local machine?

From Dev

How to display a Image on a remote linux machine through ssh

From Dev

How to checkout git sources on remote machine using local ssh key?

From Dev

How to transfer file from remote server to local machine using with ssh

From Dev

How to connect a local virtual machine to a remote server via SSH?

Related Related

  1. 1

    How to run local bash script through multiple ssh

  2. 2

    Run shell script on remote machine with SSH using local environment variable

  3. 3

    How can I execute local script on remote machine and include arguments?

  4. 4

    How to run a webserver script on remote server through SSH?

  5. 5

    How to run local shell script on remote server via SSH?

  6. 6

    How to run local shell script on remote server via SSH?

  7. 7

    Can fabric run a local script on a remote machine?

  8. 8

    Run local python script on remote machine

  9. 9

    Shell Script to run on Local and Remote machine

  10. 10

    Run local script in other machine using ssh

  11. 11

    Run a remote script on a local file via SSH

  12. 12

    How to run a local script with a multiple-word argument on a remote server?

  13. 13

    How to pass multiple arguments through ssh and use those arguments in the ssh script?

  14. 14

    Run MySQL query on remote machine through ssh in command line

  15. 15

    How to pass multiple arguments in a command to execute an exe on remote linux machine using C/C++ and SSH

  16. 16

    How to execute local script by passing arguments on remote via ssh as a different user?

  17. 17

    How to run appium script in remote machine?

  18. 18

    How to run a complex awk script on a remote machine?

  19. 19

    how to run script on remote machine with delay

  20. 20

    How to run local shell script on remote GCP machine via gcloud compute?

  21. 21

    SSH + Sudo + Expect in Bash script: Run command with sudo in remote machine

  22. 22

    Run remote R process using script on local machine

  23. 23

    When I run the script on a remote server it starts up on the local machine

  24. 24

    How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer?

  25. 25

    How to execute a shell script stored on remote machine from a local machine?

  26. 26

    How to display a Image on a remote linux machine through ssh

  27. 27

    How to checkout git sources on remote machine using local ssh key?

  28. 28

    How to transfer file from remote server to local machine using with ssh

  29. 29

    How to connect a local virtual machine to a remote server via SSH?

HotTag

Archive