Was Re: need wrapper for ssh/scp

Thomas Wouters thomas at xs4all.net
Fri Apr 7 07:09:42 EDT 2000


On Thu, Apr 06, 2000 at 09:19:32PM +0000, Andrew McDowell wrote:

> I also working with ssh to execute some remote commands on different
> servers.  I'd previously been hobbling together this strange idea of
> generating an Expect script in python then running it. (*bleh*)  So once I
> saw was clued onto the pty module I started playing with it.  Well...I'm
> either severely confused, or something isn't working properly.  Could anyone
> offer some help?

Help is easiest if you also explain what the problem is ;) I already see one
problem though: you read the password with 'string.strip(raw_input())',
which means the terminating newline gets stripped off the password, but you
dont supply it with os.write(), which means ssh thinks there is more
password to come, and patiently sits waiting for it, untill you brutally
kill it ;)

In your real app you probably want to use select(), by the way, before
attempting to read something. Doing a read() when there's no data to be read
makes a blocking read, which can lead to deadlocks. See telnetlib for some
examples on how to use it.

> Here's a sample of how I'm attempting it now:

> def remote_ex(hostname, password):

[..]

>   pid, fd = pty.fork()
>   if pid == 0:
>     os.execv("/usr/local/bin/ssh",[hostname])
>   else:
>     time.sleep(2)
>     print "Child says %s" % string.strip(os.read(fd,1024))
>     print "Child took %d password bytes." % os.write(fd, password)

[..]

> def main():
>   myhost = string.strip(raw_input("Enter a host:"))
>   mypass = string.strip(raw_input("Enter your Password:"))
>   remote_ex(myhost, mypass)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list