[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

BlackjadeLin

1/10/2008 9:00:00 AM

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


2 Answers

noah

1/10/2008 5:50:00 PM

0

On Jan 10, 12:59 am, BlackjadeLin <hoo.s...@gmail.com> wrote:
> 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

BlackjadeLin

1/13/2008 3:16:00 AM

0

On Jan 11, 1:49 am, Noah <n...@noah.org> wrote:
> On Jan 10, 12:59 am, BlackjadeLin <hoo.s...@gmail.com> wrote:
>
>
>
> > 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.