#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    9
    Rep Power
    0

    Unhappy problem with capturing errors invoked by pexpect run commands


    hello everybody,

    whn i give a pexpect command as follows:
    child=pexpect.spwan("su")
    child.expect("password")
    child.sendlines("su-password")

    the problem is that i need to capture error returned by 'su' when wrong input is given. i tried something like this:

    child=pexpect.spwan("su 2>/some/file.txt")

    but it is not working,coz the prompt then goes to file.txt (i think so..) and pexpect finds nothing for child.expect() an returns error.

    i m in urgent need of this,can somone tell how to capture errors in pexpect invokd commands.

    regards.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    9
    Rep Power
    0

    Talking got sol to my prob......


    Originally Posted by on_the_grid
    hello everybody,

    whn i give a pexpect command as follows:
    child=pexpect.spwan("su")
    child.expect("password")
    child.sendlines("su-password")

    the problem is that i need to capture error returned by 'su' when wrong input is given. i tried something like this:

    child=pexpect.spwan("su 2>/some/file.txt")

    but it is not working,coz the prompt then goes to file.txt (i think so..) and pexpect finds nothing for child.expect() an returns error.

    i m in urgent need of this,can somone tell how to capture errors in pexpect invokd commands.

    regards.
    hey ppl,

    i got solution to my problem.....it goes like this

    child=pexpect.spwan("su")
    child.expect("password")
    child.sendlines("su-password")
    i=child.expect(['su: incorrect password','$'])
    for i==0:
    print "error,incorrect password"
    child.kill(0)
    for i==1:
    print "su done"

    thanx.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by on_the_grid
    hey ppl,

    i got solution to my problem.....it goes like this
    Code:
        child=pexpect.spwan("su")
        child.expect("password")  
        child.sendlines("su-password")
        i=child.expect(['su: incorrect password','$'])
        for i==0:
                  print "error,incorrect password"
                  child.kill(0)
       for i==1:
                  print "su done"
    thanx.

    That should be
    Code:
    if i==0:
        ....
    elif i == 1:
        ....
    the 'for' statement should only be used for looping.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo