[Tutor] SSH commands in Python on Linux

Jesse Noller jnoller at gmail.com
Thu Aug 11 14:47:59 CEST 2005


On 8/10/05, Bernard Lebel <3dbernard at gmail.com> wrote:
> Hello,
> 
> I'm trying to make a script to send a SSH command from a Linux
> computer to another Linux compter.
> 
> The Python syntax I'm using...
> 
> 
> import os
> os.system( 'ssh root at 192.168.10.204 "ls"' )
> 
> 
> Now the problem is that I'm always asked for the password. So my
> question is two-fold:
> 
> 1- Is there a way to not be asked for the root password?
> 2- If not, then is it possible to feed the SSH password input with my
> Python script? I have about stdin redirection, but I have to admit
> that I'm a bit lost and I don't know if it applies to SSH input as
> well.
> 
> Right now I'm using the ls command to list processes, however
> ultimately I want to be able to kill some processes within a loop
> (this is for render farm management).
> 
> 
> 
> Thanks
> Bernard

Some of the things you are going to be bit by you've already ran into
- the first of which is authentication.

I would recommend looking at the following utilities:

http://www.theether.org/pssh/
http://sourceforge.net/projects/pyssh

Setting up private/public key authentication is going to allow for a
greate amount of secure automation. Barring that, use the pexpect
module to do the prompt handling, pexpect is bad simply due to the
fact you'll need to store your passwords plaintext within your program
which is a seriously risk.

Also note you'll need to take into account basic flow control for
Stdin, Stdout and Stderr - you need to watch for hosts that are not
found/don't exist and ssh commands that may or may not hang.

The PSSH program has excellent work arounds for all of these within it. 

-jesse


More information about the Tutor mailing list