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

Discussion in 'Python' started by BlackjadeLin, Jan 10, 2008.

  1. BlackjadeLin

    BlackjadeLin Guest

    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
     
    BlackjadeLin, Jan 10, 2008
    #1
    1. Advertising

  2. BlackjadeLin

    Noah Guest

    On Jan 10, 12:59 am, BlackjadeLin <> 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
     
    Noah, Jan 10, 2008
    #2
    1. Advertising

  3. BlackjadeLin

    BlackjadeLin Guest

    On Jan 11, 1:49 am, Noah <> wrote:
    > On Jan 10, 12:59 am, BlackjadeLin <> 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.
     
    BlackjadeLin, Jan 13, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin Huo
    Replies:
    3
    Views:
    464
    Patrice
    Oct 5, 2004
  2. yaya9

    xml tag doesn't work well

    yaya9, Mar 7, 2004, in forum: XML
    Replies:
    0
    Views:
    354
    yaya9
    Mar 7, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  4. Linuxguy123
    Replies:
    1
    Views:
    860
    Gary Duzan
    Jan 30, 2009
  5. Saqib Ali
    Replies:
    1
    Views:
    367
    Roy Smith
    Dec 26, 2011
Loading...

Share This Page