471,325 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

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

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
Jan 10 '08 #1
2 3248
On Jan 10, 12:59 am, BlackjadeLin <hoo.s...@gmail.comwrote:
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
importpexpect
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
When you call child.close() that will kill the child process.
Possibly you want to use the interact() method.
It is not clear from your message if you want to interact with
the child process as if it were your new shell. If you do,
then take a look at the interact() method.

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

--
Noah
Jan 10 '08 #2
On Jan 11, 1:49 am, Noah <n...@noah.orgwrote:
On Jan 10, 12:59 am, BlackjadeLin <hoo.s...@gmail.comwrote:
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
importpexpect
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

When you call child.close() that will kill the child process.
Possibly you want to use the interact() method.
It is not clear from your message if you want to interact with
the child process as if it were your new shell. If you do,
then take a look at the interact() method.

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

--
Noah
Thank you very much!
The interact() method complete my achievement.
Jan 13 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Axel Koehler | last post: by
6 posts views Thread by tigrfire | last post: by
3 posts views Thread by 31337one | last post: by
1 post views Thread by tereglow | last post: by
8 posts views Thread by asgarde | last post: by
11 posts views Thread by Emmanouil Angelakis | last post: by
2 posts views Thread by yellowblueyellow | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.