2

I have a problem with running non finishing process via ssh from python script. What I need to do:

  1. Connect to remote server via ssh
  2. Run mongod on this server
  3. Finish script execution without killing mongod process

For now, I'm using subprocess.Popen in this way:

subprocess.Popen(['ssh', 'user@' + host, 'mongod', '-f', '/temp-mongo.conf', '&'])

Problem is that script ends before I'm asked about user password, so it finishes with Too many authentication failures for root.

I tried to use p = subprocess.Popen(...).communicate() and it'a almost ok, but then script waits for mongod command to be finished, what obviously won't happen.

What is proper way to do this? Can I do something to pass password automatically?

1
  • start it via systemctl or if you must have a hacky way, nohup xxx & Commented Jan 13, 2017 at 9:53

2 Answers 2

2

I agree with e4c5 that you should use a tool like Fabric for that. If you want to stay with the dirty way something like this should work:

subprocess.call('ssh user@%s "mongod -f /temp-mongo.conf &>/dev/null  &"' % host, 
                shell=True)

Note that you need to do:

  • quotes around the remote call
  • add &>/dev/null which routes all output of mongod to /dev/null (without this it will block, not 100% sure why. Probably since the stdout of the shell is attached to the command)
  • use shell=True so the shell builds up the command for you (so you don't need to put a ", " instead of each space)

This also works with auth over public key (instead of writing the password by hand)

Sign up to request clarification or add additional context in comments.

6 Comments

It worked, thanks! I choose "dirty" solution, because I have limited possibilities to install new modules on remote
Fabric doesn't need any remote modules to be installed. But it is an additional learning curve (although it is quite easy to start with). My advice is that whenever you add more jobs like that to your script (and end up in a big if/else battle) or if you have jobs depending on each other, want to run jobs in parallel etc then a switch to fabric is very much worth the effort
so fabric is standard library?
no, you need to install the fabric library. But only on your client host where you start the commands, not on the servers where the commands are executed
I wrote it wrong in first comment - I'm running this script also on remote machine (from where it connect to another remote machine), so that was shorthand - best way is to not need any module.
|
0

The proper way for SSH without having to enter passwords is to use public key authentication.

I would also look at Fabric for running commands via SSH.

And you aren't running the command in a shell environment on the server. If you run

ssh host ls &

the & will actually put ssh in the background, not ls on the host. Instead try doing

ssh host sh -c 'ls &'

2 Comments

This is a comment, not an answer.
The question was how do you enter a password for the SSH connection. This is an answer. Also, I have amended more answers to all the other questions and problems OP has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.