Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using python with SSH?

0 views
Skip to first unread message

Steve

unread,
Dec 17, 2003, 6:00:48 PM12/17/03
to
Hi,

I need to run a remote program from a server that I have access rights
to. I want to be able to do this from within in my python script and I
want to use ssh (or anything else that will work). However, doing
something like:

ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine
via ssh somehow? Does a python script support this? Thanks!

Steve

Paul Rubin

unread,
Dec 17, 2003, 6:07:12 PM12/17/03
to
Steve <nospam@nopes> writes:
> ssh -l user host "/path/to/program"
>
> prompts for a password and I don't know how to supply one via a python
> script. Is there an easy way out? Can I log onto to the other machine
> via ssh somehow? Does a python script support this? Thanks!

The simplest way is to set up ssh at both ends with RSA authentication
and have a private key on the local machine. That gets rid of the
need for a password, while still keeping the connection secure, as
long as the local machine is secure.

Lawrence Oluyede

unread,
Dec 17, 2003, 6:08:43 PM12/17/03
to
Steve <nospam@nopes> writes:

> ssh -l user host "/path/to/program"
>
> prompts for a password and I don't know how to supply one via a python
> script. Is there an easy way out? Can I log onto to the other machine via
> ssh somehow? Does a python script support this? Thanks!

Try something like this:

import os

ssh_pipe = os.popen("ssh -l user path")
ssh_pipe.write(password)
ssh_pipe.write("\n")
ssh_pipe.flush()
ssh_pipe.close()

hope this helps

--
Lawrence "Rhymes" Oluyede
http://loluyede.blogspot.com

Jp Calderone

unread,
Dec 17, 2003, 7:25:42 PM12/17/03
to pytho...@python.org
On Thu, Dec 18, 2003 at 12:08:43AM +0100, Lawrence Oluyede wrote:
> Steve <nospam@nopes> writes:
>
> > ssh -l user host "/path/to/program"
> >
> > prompts for a password and I don't know how to supply one via a python
> > script. Is there an easy way out? Can I log onto to the other machine via
> > ssh somehow? Does a python script support this? Thanks!
>

You could open ssh in a pty, or you could use a Python module that speaks
the SSH protocol, thus obviating the requirement to call out to the ssh
command line program.

A couple choices for such modules:

http:/www.twistedmatrix.com/

http://www.lag.net/~robey/secsh/

> Try something like this:
>
> import os
>
> ssh_pipe = os.popen("ssh -l user path")
> ssh_pipe.write(password)
> ssh_pipe.write("\n")
> ssh_pipe.flush()
> ssh_pipe.close()
>
> hope this helps

It does not. ssh won't accept a password from standard input, it requires
it to be entered into the terminal.

Jp

Lawrence Oluyede

unread,
Dec 18, 2003, 3:34:33 AM12/18/03
to
Jp Calderone <exa...@intarweb.us> writes:

> It does not. ssh won't accept a password from standard input, it requires
> it to be entered into the terminal.

Ah ok, i thought it did.

Martin Franklin

unread,
Dec 18, 2003, 5:06:02 AM12/18/03
to Python List

Steve,


You don't say if you are running on Windows or not but this is a ssh
wrapper I use that uses the pexpect library...:


import pexpect
import sys
import re
import os


PROMPT = "\$|\%|\>"


class SSH:
def __init__(self, user, password, host):
self.child = pexpect.spawn("ssh %s@%s"%(user, host))
i = self.child.expect(['assword:', r"yes/no"], timeout=120)
if i==0:
self.child.sendline(password)
elif i==1:
self.child.sendline("yes")
self.child.expect("assword:", timeout=120)
self.child.sendline(password)
self.child.expect(PROMPT)


def command(self, command):
"""send a command and return the response"""
self.child.sendline(command)
self.child.expect(PROMPT)
response = self.child.before
return response

def close(self):
"""close the connection"""
self.child.close()


if __name__=="__main__":
import getpass
password = getpass.getpass("Password: ")
ssh = SSH("RemoteUsername", password, "RemoteHost")
print ssh.command("pwd")
ssh.close()


--
Martin Franklin <mfran...@gatwick.westerngeco.slb.com>


Albert Hofkamp

unread,
Dec 18, 2003, 9:46:20 AM12/18/03
to
On Thu, 18 Dec 2003 10:06:02 +0000, Martin Franklin <mfran...@gatwick.westerngeco.slb.com> wrote:
> On Wed, 2003-12-17 at 23:00, Steve wrote:
>> Hi,
>>
>> I need to run a remote program from a server that I have access rights
>> to. I want to be able to do this from within in my python script and I
>> want to use ssh (or anything else that will work). However, doing
>> something like:
>>
>> ssh -l user host "/path/to/program"
>>
>> prompts for a password and I don't know how to supply one via a python
>> script. Is there an easy way out? Can I log onto to the other machine

If your sysadmin allows it, you can configure ssh to connect to another
machine without prompting for a password, at least at Linux systems.


Albert
--
Unlike popular belief, the .doc format is not an open publically available format.

eic...@metacarta.com

unread,
Dec 21, 2003, 6:51:49 PM12/21/03
to
I use python-expect for this. Note that python-expect is not
particularly good (gets the trailing flush case wrong sometimes,
doesn't actually behave like 'sh -c' even though the documentation
claims it does) and un(der)documented, but the basic use of
expect.popen2 on a single command (such as you described) works just
fine. It works fairly well to use this under qmtest - you put the
hostname (and perhaps password) of the machine-under-test into your
context file, then you have standard tests that build on top of a
basic "scp/ssh over to that machine, compare the results with this
regexp/string" and you can get pretty far with that.
0 new messages