still trying to pass strings to the OS

B

Bart Nessux

I posted on this topic a few days ago, got a tip to use popen to
redirect stdin. Here's where I'm at now:

import popen2
r,w,e = popen2.popen3("/usr/bin/passwd")
print r.readline()
w.write("password")
w.flush()

When this is ran, the shell responds with "New password:" at the console
and then nothing happens. I can key in the password and then confirm it
manually after which "print r.readline()" does its thing by printing out
"Changing password for user." Note that when the command is ran from the
shell manually "Changing password for user." is the first thing printed
to the console. So, here's my questions:

1. Why does my readline come last?
2. Also, when I do more than one readline, I get nothing, why is this?
I should be able to read "New password:" and "Retype new password:"
from the process.
3. Why is my write being ignored?
 
B

Bart Nessux

Cameron said:
If you can get your hands on a copy of the February 2004 issue of
*Sys Admin* Magazine <URL: http://www.samag.com/articles/2004/0402/ >,
you'll find a bit of encouragement there to pursue Pexpect.

After downloading and installing pexpect... this works for me:

import pexpect

password = 'password'
child = pexpect.spawn('/usr/bin/passwd root')
child.expect_exact('New password:')
child.sendline(password)
child.expect_exact('Retype new password:')
child.sendline(password)

Does this look safe? The documentation on the module was lacking. Seems
like I should close the process that was spawned. Any tips?
 
B

Bart Nessux

Bart said:
After downloading and installing pexpect... this works for me:

import pexpect

password = 'password'
child = pexpect.spawn('/usr/bin/passwd root')
child.expect_exact('New password:')
child.sendline(password)
child.expect_exact('Retype new password:')
child.sendline(password)

Does this look safe? The documentation on the module was lacking. Seems
like I should close the process that was spawned. Any tips?

This works 100% on Mac OSX 10.3.2... The time.sleeps had to be added to
make it work consistently.

def set_pass():
# Function to change root passwd on Mac 10.3 systems
import pexpect
import time
set = r"password"
child = pexpect.spawn("/usr/bin/passwd")
child.expect_exact("New password:")
child.sendline(set)
time.sleep(0.1)
child.expect_exact("Retype new password:")
child.sendline(set)
time.sleep(0.1)
child.close()

set_pass()
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top