1

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?

Jesse
  • 121
  • 6

2 Answers2

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

Jesse
  • 121
  • 6
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

Dylan Martin
  • 548
  • 4
  • 14