0

I'm trying to invoke a bash script on a container on a remote VM like below,

sshpass -p password ssh ubunt@${slave_ip} "kubectl exec -it pod_name -c container_name -- bash -c "/my_script.sh --${version} --${remote_ip} --${password} --${range} --${env}""

Problem being, only the 1st argument - version gets parsed and the rest are lost.

What am I doing wrong & how to pass multiple arguments correctly using kubectl, many thanks.

Goku
  • 482
  • 1
  • 7
  • 22
  • 1
    You can't nest quotes like that. You can switch double quotes for single in one of the pairs, but which to change depends on where you hope the values for `$version`, `$remote_ip` etc to come from. If they are defined in the local shell where you run `ssh`, you want double quotes around the whole thing to expose those. In the opposite case, you want single quotes around the whole script, and double quotes in the `bash -c` argument. (For what it's worth, the braces around the variable names are not doing anything useful here, and are making things slightly harder on the eyes.) – tripleee Nov 04 '22 at 13:34

1 Answers1

1

This should work better :

sshpass -p password ssh ubunt@${slave_ip} "kubectl exec -it pod_name -c container_name -- bash -c '/my_script.sh --${version} --${remote_ip} --\"${password}\" --${range} --${env}'"

even though it can still have problems if you $password contains special characters like quotes, so try to think of different ways to pass it.

Update

I tested with sshpass, which worked as well.

Can you run following and post result ? (Notice the printf %s__)

sshpass -p password ssh ubunt@${slave_ip} "kubectl exec -it pod_name -c container_name -- bash -c 'printf %s__ /my_script.sh --${version} --${remote_ip} --\"${password}\" --${range} --${env}'"
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • My bad I didnt observe the problem completely, actually none of the arguments gets passed to the script inside the container right from `version`. – Goku Nov 02 '22 at 04:20
  • I tested with public key authentication. Can you setup public key authentication ? – Philippe Nov 02 '22 at 20:28
  • @Philipe : Unfortunately, it cant be done.. its a small piece of a bigger builder process :( – Goku Nov 03 '22 at 03:54
  • @Philipe: Sorry, could not get back to you earlier. Now I see the values are getting passed properly but there is `double underscore` before every argument, why so ?like, `/run_tests.sh__--5.0.0__--10.44.5.1__--User123__--API__--DEV__` – Goku Nov 08 '22 at 08:55
  • Double underscore is from `printf %s__`, just to separate the values. – Philippe Nov 08 '22 at 09:33
  • @Philipe : Really appreciate your time here !! its working great :) – Goku Nov 08 '22 at 12:29