Why my program (using pexpect to switch user) doesn't work well?

BlackjadeLin hoo.smth at gmail.com
Thu Jan 10 03:59:43 EST 2008


I'm new to python
I want to write a simple script to switch user,for example,from user_A
to user_B.
This my codes:

#!/usr/bin/python
import pexpect
import os
passwd="user_B"
child = pexpect.spawn('su user_B')
child.expect('Password:')
child.sendline(passwd)
child.expect('$')
child.close()

Maybe it's the easiest pexpect program.Sometimes ,it work well,it
switch to user_B successfully .But after i type the command exit to
switch back to user_A,execute the python script again,it can't work,do
nothing or just waiting.Why it have different results?
Sorry for my poor English,and many thanks to all.

Blackjade





More information about the Python-list mailing list