0

From all of my testing it seems like it is not possible to execute a local script remotely while allowing an interactive shell and passing arguments.

Interactive (arguments try to run as a seperate command either inside or outside the double quotes)

ssh -t server "$(<${scriptname})"

Arguments (Not interactive ofc)

cat $scriptname | ssh -t server bash -s - "${args[@]}"

I have really tried this every which way. Is it possible to both start an interactive shell and execute the script while also sending arguments?

1

I believe to have answered my own question by considering changing the values of of the $@ arguments on the remote to the ones locally passed and it seems to work in my tests.

Here is my final solution on just switching out $1 and $2

Local Script

#!/bin/bash

echo "I wanna read you $1 and $2"
read yn

if [ "$yn" == "y" ]; then
    echo "I read you"
else
    echo "I could not read you"
fi

Command

ssh -t $server "set -- "${@:1:2}"; $(<${script})"

Result (when sending test1 and test2 as arguments)

I wanna read you test1 and test2

y

I read you!

Hopefully this helps any future visitors

0

As an academic question, I'm sure if you are a wizard at shell escaping and parameter expansion, you could do it. I wouldn't recommend it though.

As a practical matter, I do:

scp script.sh me@server; ssh me@server "./script.sh"

pretty often.

If you want to do real work remotely over ssh, you should look into ansible

3
  • Hi there. I was definitely familiar with Ansible. I believe to have answered my own question however after logically giving it some more thought. Thanks for your suggestions! – Jesse Feb 9 '18 at 0:04
  • Oh come on! What was the answer? – Dylan Martin Feb 23 '18 at 1:33
  • The answer is the ... answer. Right above the one you commented on – Jesse Feb 24 '18 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.