[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: ssh
From: Karthik Gurusamy <kar1107 () gmail ! com>
Date: 2008-04-30 23:20:58
Message-ID: 9216be73-c835-4eaf-b90b-b7783f5a4770 () r9g2000prd ! googlegroups ! com
[Download RAW message or body]
On Apr 29, 6:29 pm, gert <gert.cuyk...@gmail.com> wrote:
> Is this the best way to use ssh ?
> How can i use ssh keys instead of passwords ?
> I dont understand what happens when pid does not equal 0 , where does
> the cmd get executed when pid is not 0 ?
> How do you close the connection ?
>
> #http://mail.python.org/pipermail/python-list/2002-July/155390.html
> import os, time
>
> def ssh(user, rhost, pw, cmd):
> pid, fd = os.forkpty()
> if pid == 0:
> os.execv("/bin/ssh", ["/bin/ssh", "-l", user, rhost] + cmd)
> else:
> time.sleep(0.2)
> os.read(fd, 1000)
> time.sleep(0.2)
> os.write(fd, pw + "\n")
> time.sleep(0.2)
> res = ''
> s = os.read(fd, 1)
> while s:
> res += s
> s = os.read(fd, 1)
> return res
>
> print ssh('username', 'serverdomain.com', 'Password', ['ls -l'])
If ssh is being used interactively (such as being prompted from
password), look into pexpect module.
http://www.noah.org/wiki/Pexpect
Else try the subprocess module (or even commands module); these are
lot simpler to program than the more primitive os.execv/os.read/write.
If you have already setup keys, ssh should work passwordless whether
it's interactive or not (AFAIK).
Karthik
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic