forkpty not working?

est electronixtar at gmail.com
Thu Mar 20 03:40:49 EDT 2008


Hello everyone. I am trying to write a bash/ssh wrapper in python so
python scripts could interact with bash/ssh.

Because when input passwords to ssh it requires a tty rather than
stdin pipe, so i have to use a pty module to do that.

I copied this snippet from this thread
http://groups.google.com/group/comp.lang.python/browse_thread/thread/6bbc3d36b4e6ff55/

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)
                x=open("it_worked.txt", "w") #output a file for test
                x.write("xxx")
                x.close()
        #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

As far as I can see the code is not working, when called the function
rcmd() there is no file it_worked.txt spawned in the directory. I am
n00b to POSIX, so anyone please give me some hint? what happened when
os.forkpty()?



More information about the Python-list mailing list