forkpty not working?

E

est

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()?
 
D

Dan Stromberg

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.
 
E

est

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.

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()?- Hide quoted text -

- Show quoted text -

Thanks for the reply! What's your recommandation for a bash wrapper? I
am trying to implement some simple stuff which functions like ipython.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top