tags:

views:

498

answers:

1

I want to connect too and execute a process on a remote server using python. I want to be able to get the return code and stderr(if any) of the process. Has anyone ever done anything like this before. I have done it with ssh, but I want to do it from python script.

Cheers.

+10  A: 

Well, you can call ssh from python...

import subprocess
ret = subprocess.call(["ssh", "user@host", "program"]);

# or, with stderr:
prog = subprocess.Popen(["ssh", "user@host", "program"], stderr=subprocess.PIPE)
errdata = prog.communicate()[1]
sth
You'll need to make sure that you have key-based authentication to the remote host, and the proper keys. Otherwise, you'll be prompted for a password.
slacy
nice, apparently I'm out of date - was going to recommend os.spawnlp, but i see the subprocess module was introduced to replace it.
klochner
Thanks dude. When you exec a command over ssh the return code and stderr is that of the exec'd command, if ssh doesn't have any issues itself. I can the popen class and the communicate() method to tap into it from there.
stinkypyper