Grupy dyskusyjne Google nie obsługują już nowych postów ani subskrypcji z Usenetu. Treści historyczne nadal będą dostępne.

ssh and Python

297 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

edadk

nieprzeczytany,
21 lip 2002, 06:21:0921.07.2002
do
Hi,

There is a telnet and ftp object for Python. But how about ssh?

I would like to write script that using ssh automatically execute
commands commands on a remote machine. How can I do that?

Erling

Chris Liechti

nieprzeczytany,
21 lip 2002, 07:37:1721.07.2002
do
e.d.an...@mosek.com (edadk) wrote in
news:ebc0c915.02072...@posting.google.com:

> There is a telnet and ftp object for Python. But how about ssh?

often modules are named py... and Google is your friend, so lets search
for "pyssh":

http://www.google.com/search?q=pyssh

chris


--
Chris <clie...@gmx.net>

John Hunter

nieprzeczytany,
21 lip 2002, 14:34:3221.07.2002
do
>>>>> "edadk" == edadk <e.d.an...@mosek.com> writes:

edadk> I would like to write script that using ssh automatically
edadk> execute commands commands on a remote machine. How can I do
edadk> that?

For simple stuff, you have at your disposal, for example:

import os
h = os.popen('ssh -2 somehost.com ls')
print h.read()

John Hunter

Bo M. Maryniuck

nieprzeczytany,
22 lip 2002, 06:19:2822.07.2002
do
On Sunday 21 July 2002 20:34, John Hunter wrote:
> import os
> h = os.popen('ssh -2 somehost.com ls')
> print h.read()

That's solves half of problem. How to automatically pass the password? Yes,
you need a pipe to write in...

--
Sincerely yours, Bogdan M. Maryniuck

Microsoft is not the answer.
Microsoft is the question.
NO (or Linux) is the answer.
(Taken from a .signature from someone from the UK, source unknown)

Erno Kuusela

nieprzeczytany,
22 lip 2002, 07:12:2022.07.2002
do
In article <mailman.1027333171...@python.org>, "Bo
M. Maryniuck" <b.mar...@forbis.lt> writes:

| On Sunday 21 July 2002 20:34, John Hunter wrote:
|| import os
|| h = os.popen('ssh -2 somehost.com ls')
|| print h.read()

| That's solves half of problem. How to automatically pass the password? Yes,
| you need a pipe to write in...

use a rsa key with ssh-agent (or without passphrase if you need
it to run unattended).

-- erno


Bo M. Maryniuck

nieprzeczytany,
22 lip 2002, 08:19:4322.07.2002
do
On Monday 22 July 2002 13:12, Erno Kuusela wrote:
> use a rsa key with ssh-agent (or without passphrase if you need
> it to run unattended).

But sometimes you just need to pass a password.

--
Sincerely yours, Bogdan M. Maryniuck

"Are [Linux users] lemmings collectively jumping off of the cliff of
reliable, well-engineered commercial software?"
(By Matt Welsh)

Erno Kuusela

nieprzeczytany,
22 lip 2002, 08:28:5722.07.2002
do
In article <mailman.1027340370...@python.org>, "Bo
M. Maryniuck" <b.mar...@forbis.lt> writes:

| On Monday 22 July 2002 13:12, Erno Kuusela wrote:
|| use a rsa key with ssh-agent (or without passphrase if you need
|| it to run unattended).

| But sometimes you just need to pass a password.

hmm? you can always use a rsa key, unless the administrator
has disabled rsa keys (which would be silly since hardcoding
passwords is less safe).

-- erno

iwk

nieprzeczytany,
22 lip 2002, 10:39:0922.07.2002
do edadk
edadk wrote:
> I would like to write script that using ssh automatically execute
> commands commands on a remote machine. How can I do that?

Use the following function 'rcmd' to execute commands on a server
through SSH with password authentication (better to use keys, but that's
not always an option)

The trick is to use os.forkpty because ssh will not read stdin for the
password, but rather directly from the terminal.

It works on Unix and on Windows through Cygwin (not directly on Win32
because it lacks the os.forkpty() implementation)

Regards,

Iwan

#!/usr/bin/env python
#Remote command through SSH using user/password
import os, time

def pause(d=0.2):
time.sleep(d)

def rcmd(user, rhost, pw, cmd):
#Fork a child process, using a new pseudo-terminal as the child's
controlling terminal.
pid, fd = os.forkpty()
# If Child; execute external process
if pid == 0:
os.execv("/bin/ssh", ["/bin/ssh", "-l", user, rhost] + cmd)
#if parent, read/write with child through file descriptor
else:
pause()
#Get password prompt; ignore
os.read(fd, 1000)
pause()
#write password
os.write(fd, pw + "\n")
pause()
res = ''
#read response from child process
s = os.read(fd,1 )
while s:
res += s
s = os.read(fd, 1)
return res

#Example: execute ls on server 'serverdomain.com'
print rcmd('username', 'serverdomain.com', 'Password', ['ls -l'])


Andy Salnikov

nieprzeczytany,
22 lip 2002, 19:02:4522.07.2002
do

"Erno Kuusela" <erno...@erno.iki.fi> wrote in message
news:kuofd03...@lasipalatsi.fi...

I know one example when one has to use plain password in ssh.
This is when you log on onto AFS-enabled host from non-afs machine
(or using ssh without AFS support, which is standard on Windows).
In this case using RSA you'll not get your AFS token and then you'll
need to execute 'klog' manually an type in your password anyway.

Cheers,
Andy.


Erno Kuusela

nieprzeczytany,
23 lip 2002, 03:14:1523.07.2002
do
In article <ahi2up$fm9$1...@usenet.Stanford.EDU>, "Andy Salnikov"
<a_sal...@yahoo.com> writes:

| "Erno Kuusela" <erno...@erno.iki.fi> wrote in message

|| hmm? you can always use a rsa key, unless the administrator
|| has disabled rsa keys (which would be silly since hardcoding
|| passwords is less safe).
||

| I know one example when one has to use plain password in ssh.
| This is when you log on onto AFS-enabled host from non-afs machine
| (or using ssh without AFS support, which is standard on Windows).
| In this case using RSA you'll not get your AFS token and then you'll
| need to execute 'klog' manually an type in your password anyway.

oh, right. i suppose it would be the same with many of the
other non-unix-password based authentication methods.

-- erno


Bo M. Maryniuck

nieprzeczytany,
23 lip 2002, 04:02:5123.07.2002
do
On Monday 22 July 2002 14:28, Erno Kuusela wrote:
> unless the administrator has disabled rsa keys
That's what I'm talking about.

--
Sincerely yours, Bogdan M. Maryniuck

A Linux machine! because a 486 is a terrible thing to waste!
(By j...@wintermute.ucr.edu, Joe Sloan)

Erno Kuusela

nieprzeczytany,
23 lip 2002, 07:00:4623.07.2002
do
In article <mailman.102741142...@python.org>, "Bo
M. Maryniuck" <b.mar...@forbis.lt> writes:

| On Monday 22 July 2002 14:28, Erno Kuusela wrote:
|| unless the administrator has disabled rsa keys
| That's what I'm talking about.

that probably means that they have disabled them because they
fear people will use passphraseless rsa keys. i suggest
you have a chat with the administrator, as hardcoding
the password is a worse solution.

-- erno

Bo M. Maryniuck

nieprzeczytany,
23 lip 2002, 09:05:2023.07.2002
do
On Tuesday 23 July 2002 13:00, Erno Kuusela wrote:
> that probably means that they have disabled them because they
> fear people will use passphraseless rsa keys. i suggest
> you have a chat with the administrator, as hardcoding
> the password is a worse solution.

Yeah, I agree. Fully. But life in the computer science sometimes sucks...

--
Sincerely yours, Bogdan M. Maryniuck

Microsoft Corp., concerned by the growing popularity of the free 32-bit
operating system for Intel systems, Linux, has employed a number of top
programmers from the underground world of virus development. Bill Gates stated
yesterday: "World domination, fast -- it's either us or Linus". Mr. Torvalds
was unavailable for comment ...
(r...@swift.eng.ox.ac.uk (Robert Manners), in comp.os.linux.setup)

Nowe wiadomości: 0