I have a series of bash commands, some with interactive prompts, that I need run on a remote machine. I have to have them called in a certain order for different scenarios, so I've been trying to make a bash script to automate the process for me. However, it seems like every way to start an ssh
session with a bash script results in the the redirection of stdin
to whatever string or file was used to initiate the script in the first place.
Is there a way I can specify that a certain script be executed on a remote machine, but also forward stdin
through ssh
to the local machine to enable the user to interact with any prompts?
Here's a list of requirements I have to clarify what I'm trying to do.
- Run a script on a remote machine.
- Somewhere in the middle of that remote script be command that will prompt for input. Example:
git commit
will bring upvim
.- If that command is
git commit
and it brings upvim
, the user should be able to interact withvim
as if it was running locally on their machine. - If that command prompts for a
[y/n]
response, the user should be able to input their answer.
- If that command is
- After the user enters the necessary information—by quitting
vim
or pressing return on a prompt—the script should continue to run like normal. - My script will then terminate the ssh session. The end product is that commands were executed for the user without them needing to be aware that it was through a remote connection.
I've been testing various different methods with the following script that I want run on the remote machine.
#!/bin/bash
echo hello
vim
echo goodbye
exit
It's crucial that the user be able to use vim
, and then, when the user finishes, "goodbye" should be printed to the screen and the remote session should be terminated.
I've tried uploading a temporary script to the remote machine and then running ssh user@host bash /tmp/myScript
, but that seems to also take over stdin
completely, rendering it impossible to let the user respond to prompts for user input. I've tried adding the -t
and -T
options (I'm not sure if they're different), but I still get the same result.
One commenter mentioned using expect
, spawn
, and interact
, but I'm not sure how to use those tools together to get my desired behavior. It seems like interact
will result in the user gaining control over stdin
, but then there's no way to have it relinquished once the user quits vim
in order to let my script continue execution.
Is my desired behavior even possible?