forkpty not working?

Dan Stromberg dstromberglists at gmail.com
Thu Mar 20 14:58:11 EDT 2008


If you google a bit, I believe you'll find one or more python modules for 
working with ssh.

Also, if you want to roll your own, the easiest way to get around the 
password requirement is to use ssh passwordless authentication using DSA 
or RSA public/private keypairs:

http://stromberg.dnsalias.org/~dstromberg/ssh-keys.html

As far as why the code below doesn't open your "it_worked" file - exec 
replaces the current process with the specified process - so the current 
process effectively goes away on exec.

On Thu, 20 Mar 2008 00:40:49 -0700, est wrote:

> 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