Bash: interactive remote prompt

Try something like this:

$ ssh -t yourserver "$(<your_script)"

The -t forces a tty allocation, $(<your_script) reads the whole file and in this cases passes the content as one argument to ssh, which will be executed by the remote user's shell.

If the script needs parameters, pass them after the script:

$ ssh -t yourserver "$(<your_script)" arg1 arg2 ...

Works for me, not sure if it's universal though.


Your problem is that ssh starts a login, non-interactive shell on the remote machine. The obvious easy solution would be to copy the script to the remote server and run it from there:

scp myscript.sh root@server:/tmp && ssh root@server /tmp/myscript.sh

If copying is not an option for whatever reason, I would modify the script to first connect and check if $1 is installed, then reconnect and install as necessary:

OUT=$(ssh root@server rpm -qa | grep "$1");
if [ "$OUT" != "" ] ; then
    echo "$1 already installed"
else
   read -p "Package $1 is not installed. Do you want to install it (y/n)?" choice
   if [ "$choice" -eq "y" ]; then
       ssh root@server yum install "$1"
   fi
fi