2

I am trying to use Fabric to run commands on a remote machine.

This works fine, until the command on the remote machine is interactive. In that case, Fabric return the interactive shell, but force me to type the info needed, while I am trying to send a command that does everything in remote, so I can automate the procedure.

Example:

from fabric.api import *
env.hosts=['myhost.mydomain']
env.user='root'
run(test1/myapp; exectask; exit)

I run a cli application on a remote machine, that uses interactive shell, so it is waiting for me to type the command (exectask); then once done, to exit, I call the exit command.

What happens now is that the app launch, Fabric show me the interactive UI and I still need to type exectask and after, exit.

How can I tell fabric to run that app, then pass the command to the interactive shell and then the exit to quit?

I see that Fabric has the prompt feature, but that's to ask the user to input data, while I want to just pass the commands and get the result back.

  • have you looked into use an input redirect i.e. **mycommand – JL Peyret Apr 27 '16 at 00:21
  • fabric has an open_shell operation, not sure if operations are something that can be used for this, are they? https://docs.fabfile.org/en/1.8/api/core/operations.html#fabric.operations.open_shell – fuzzyTew Jun 27 '22 at 17:32

1 Answers1

1

You might want to look into pexpect, a pure-Python module that works like expect. Essentially, it allows your program to spawn an external program or process, then control it just like a human was interacting with it. You program in what the program should expect to see (hence the name), then what action(s) to take.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Thanks Matt, so it is not possible to execute such actions with Fabric? It is strange that on the web, nobody uses Fabric for such task, so before abandon it, and use something else, I want to be sure that it cannot be done with Fabric. –  Mar 30 '15 at 20:26
  • 1
    @newbiez honestly, I've only used Fabric for some pretty basic tasks - nothing that requires interactivity. I just glanced through [docs.fabfile.org](http://docs.fabfile.org) and didn't see anything immediately useful, but I didn't look that hard. I agree, it *should* be possible, so perhaps a deeper reading of the docs or googling will yield better results. – MattDMo Mar 30 '15 at 20:41
  • @newbiez were you ever able to use Fabric in this way? I am trying to do the same thing now, and having similar issues. It after connecting to the remote machine, and starting the remote program, it doesn't seem to recognize the prompt, it hangs because the previous command never 'finished' – Andy Poquette Feb 17 '17 at 23:38