forkpty not working?

est electronixtar at
Thu Mar 20 08:40:49 CET 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

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] +
                x=open("it_worked.txt", "w") #output a file for test
        #if parent, read/write with child through file descriptor
                #Get password prompt; ignore
      , 1000)
                #write password
                os.write(fd, pw + "\n")
                res = ''
                #read response from child process
                s =,1 )
                while s:
                        res += s
                        s =, 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

More information about the Python-list mailing list