Was Re: need wrapper for ssh/scp

Andrew McDowell drew.mcdowell at msfc.nasa.gov
Thu Apr 6 23:19:32 CEST 2000


Thomas Wouters <thomas at xs4all.net> wrote:
>ssh/scp (and indeed most programs that ask for a password) talk directly to
>the tty when getting sensitive information (like the password or the
>passphrase.) You can catch this by using the 'pty' module. You probably
want
>to pty.fork(), and then redirect stdin/stdout, and fiddle with the terminal
>on both ends to easily pass the passphrase... You face the usual deadlock
>problems though, when both ends of the pipe are waiting for the other to
>send data.

Umm....Help? :)

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?
Here's a sample of how I'm attempting it now:

#!/usr/local/bin/python

import os, string, pty, time, signal

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)
    time.sleep(4) #being over cautious
    print "Child took %d command bytes" % os.write(fd, "echo $HOST >
ptyhost.txt")
    print "Child took %d exit bytes." % os.write(fd, "exit")
    os.kill(pid, signal.SIGKILL)
  return

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

##########################

if __name__ == "__main__":
  main()





More information about the Python-list mailing list