Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why my program (using pexpect to switch user) doesn't work well?

Reply
Thread Tools

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

 
 
BlackjadeLin
Guest
Posts: n/a
 
      01-10-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


 
Reply With Quote
 
 
 
 
Noah
Guest
Posts: n/a
 
      01-10-2008
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
 
Reply With Quote
 
 
 
 
BlackjadeLin
Guest
Posts: n/a
 
      01-13-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the system clock with pexpect confuses pexpect! Saqib Ali Python 1 12-26-2011 01:51 PM
Why doesn't this work in Eclipse ? (Simple pexpect code thatworks in bash) Linuxguy123 Python 1 01-30-2009 05:21 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Get pexpect to work Jurian Sluiman Python 2 10-29-2006 09:26 PM
cannot get pexpect to work Andrei Python 5 08-30-2003 07:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57