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
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>
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
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)
| 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
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)
| 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
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'])
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" <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
--
Sincerely yours, Bogdan M. Maryniuck
A Linux machine! because a 486 is a terrible thing to waste!
(By j...@wintermute.ucr.edu, Joe Sloan)
| 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
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)