0

I wrote a bash script that at one point automates the installation of some software on a remote host, like so:

ssh user@remotehost "<path>/install-script"

Where install-script is a bash script. That bash script at some point calls another bash script, which at some point calls an interactive python script, which then uses python's raw_input() function to gather user input.

When I run the install script normally (from a bash shell), it prompts for and accepts input perfectly fine. However, when the above piece of code from my script runs, I get no prompt until after I type the input.

The only script I really have control over is my automation script.

I have read this question: "Python - how can I read stdin from shell, and send stdout to shell and file." However, I have no problem running in a normal bash shell, only via a remote command over ssh.

Can this issue be fixed from within my script (and if so, how?), or would I have to modify the python script?

UPDATE

To clarify, the input prompts I am referring to are the prompts from the python script. I am not entering a password for ssh (the remote host has my public key in it's authorized_keys file).

UPDATE

For further clarification, my script (bash) is calling the install script (bash) that calls another bash script that finally calls the python script, which prompts for user input.

i.e. bash -> bash -> bash -> python

Community
  • 1
  • 1
rom58
  • 103
  • 2
  • 6
  • You need to use subprocess.Popen or a ssh lib in order to achieve the input part. – Torxed Jan 22 '14 at 07:24
  • Something along the lines of the code in my issue: http://stackoverflow.com/questions/20472288/python-ssh-password-auth-no-external-libraries-or-public-private-keys/20472419#20472419 – Torxed Jan 22 '14 at 07:25
  • I'm aware, however the code in my problem handles input/ouput of a script via SSH. Note the `elif 'are you sure you want to continue connecting' in lower:` which is a example of when i give input back to the remote prompt. I've used it on remote installation scripts requiring some form of action/input from the user. – Torxed Jan 22 '14 at 07:37
  • So you are saying that I would have to modify the python script (the one that is prompting), rather than my bash script? – rom58 Jan 22 '14 at 07:45
  • I might have mixed things around, I assumed the calling script was Python and that the remote script was bash->bash->python. But you're saying your calling script is bash? – Torxed Jan 22 '14 at 07:46
  • Yes. Sorry, I updated my question (I had forgotten to specify what sort of script my script was). – rom58 Jan 22 '14 at 07:48

1 Answers1

1
ssh user@remotehost "<path>/install-script"

When you run ssh and specify a command to invoke on the remote system, by default ssh will not allocate a PTY (pseudo-TTY) for the remote session. This means that you will communicate with the remote process through a set of pipes rather than a TTY.

When writing to a pipe, unix programs will typically buffer their write operations. You won't see output written to a pipe until the process writing to the pipe flushes its buffer, or when it exits--because buffered output is normally flushed on exit. Beyond that, a process can detect whether it's writing to a file, pipe, or tty, and it may adjust its behavior. For example, a shell like bash won't print command prompts when reading commands from a pipe.

You can force ssh to request a TTY for the session using the -t option:

ssh -tt user@remotehost "<path>/install-script"

Refer to the ssh man page for details.

Kenster
  • 23,465
  • 21
  • 80
  • 106