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

    Join Date
    Mar 2005
    Posts
    9
    Rep Power
    0

    Smile giving input to a exe called by os.system()


    hello ppl,

    i m developing a GUI using python and pyGTK. the problem i m facing is that there is a command called grid-proxy-init,for which i developed a window using pyGTK. this command prompts the user for a password.
    now whenever i m clicking the button which executes os.system('grid-proxy-init') the prompt apprears on the terminal(i m using red hat linux 8). is there some way in which i can give the input to the program, via the window. i have tried redirections, but that hardly seems to work.

    i have gone completely clueless ,help will be highly appreciated.
    regards.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The system() function simply runs the program and exists, returning the exit code. If the command prompts for input then you could use popen() to send the text entered in the window to the running process.

    You could also prompt the user then send the password a long with the command. This way is probably the easiest depending on what your doing exactly. You send the command with os.system() as you would on the command line:

    Code:
    ...
    os.system('grid-proxy-init %s %s' % (username, password))
    ...
    If you are using Python2.4 then you should use the subprocess module for this instead!

    Hope this helps

    Mark.
    programming language development: www.netytan.com Hula

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

    Join Date
    Mar 2005
    Posts
    9
    Rep Power
    0

    Lightbulb problem finding material on popen()


    hello mark,
    thanx for the help. i had been trying to mail back since but cud'nt coz lost the password .
    i read many articles on os.popen() but cud'nt find wht i needed. i tried following code::

    import os
    f=os.popen('c program prompting for password')
    f.write('password' )


    it is working if i write my own c program, but not for grid-proxy-init.
    sending parameters via os.system() won't work. i tried tht earlier. coz tht takes only arguments,not input. it is something like making a window(GUI) for su command in linux. where i want to pass input(password) from the window.
    if u can suggest some material to read on os.popen(), it wud be nice of u.

    regards.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey Grid,

    I only have a few minutes so this is going to be a short reply sorry. This would normally be done with subprocesses but for longevity here's an example which uses popen2 module .

    Code:
    >>> import popen2
    >>> (stdo, stdi) = popen2.popen2('sudo ls')
    >>> stdi.write('password')
    >>> print stdo.read()
    .CFUserTextEncoding
    .DS_Store
    .Trash
    .Xauthority
    .bash_history
    .fonts.cache-1
    .gaim
    .gimp-2.2
    .java
    .lpoptions
    .profile
    .ssh
    .viminfo
    Desktop
    Documents
    Downloads
    Library
    Movies
    Music
    Pictures
    Projects
    Public
    Sites
    
    >>>
    I would really consider rewriting this to use the subprocesses module though. The basic idea is there anyway, sudo prompts for a password, which I supply though stdin. I can then read the commands results .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The popen family of functions are good for running programs that act as filters - ie they read a stream of data from stdin and write a stream of data to stdout.

    Where they fall down is in the situation you have - interactive programs where you send some text, and then wait for a response before sending a reply, and so on. This does not work with popen for two reasons: firstly because stdin and stdout are buffered so any attempt at interaction is going to have sychronisation problems. Secondly (and is the problem you are having) is that passwords are not read from stdin, but direct from the tty. This is so that the password is not echoed back to the sender.

    There is a way round this - use pexpect. this is a python port of the TCL-based expect program, and will do exactly what you want. It creates a pseudo-tty so you can send the password, and has functions for controlling the child process interactively.

    NOTE: This does not work on standard Windows python since the OS does not support the functions needed by it. It will work in cygwin however.

    Dave - The Developers' Coach
    Last edited by DevCoach; March 24th, 2005 at 03:51 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11

    I think that's exactly what he needs. You are always on target devcoach


    I agree completly.

    Originally Posted by DevCoach
    The popen family of functions are good for running programs that act as filters - ie they read a stream of data from stdin and write a stream of data to stdout.

    Where they fall down is in the situation you have - interactive programs where you send some text, and then wait for a response before sending a reply, and so on. This does not work with popen for two reasons: firstly because stdin and stdout are buffered so any attempt at interaction is going to have sychronisation problems. Secondly (and is the problem you are having) is that passwords are not read from stdin, but direct from the tty. This is so that the password is not echoed back to the sender.

    There is a way round this - use pexpect. this is a python port of the TCL-based expect program, and will do exactly what you want. It creates a pseudo-tty so you can send the password, and has functions for controlling the child process interactively.

    NOTE: This does not work on standard Windows python since the OS does not support the functions needed by it. It will work in cygwin however.

    Dave - The Developers' Coach
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    9
    Rep Power
    0

    help with pexpect


    hello all,
    i tried pexpect. but it is not working . my code is as follows::

    #!/usr/bin/env python
    import pexpect
    import getpass

    password = getpass.getpass('Password: ')

    child = pexpect.spawn("su")

    child.expect('password:')
    child.sendline(password)

    child.expect(pexpect.EOF)

    print child.before

    i get the following error::::

    File "/usr/lib/python2.2/site-packages/pexpect.py", line 631, in expect
    return self.expect_list(compiled_pattern_list, timeout)
    File "/usr/lib/python2.2/site-packages/pexpect.py", line 736, in expect_list
    c = self.read_nonblocking (self.maxread, timeout)
    File "/usr/lib/python2.2/site-packages/pexpect.py", line 326, in read_nonblocking
    raise TIMEOUT('Timeout exceeded in read().')
    pexpect.TIMEOUT: 'Timeout exceeded in read().'

    i tried the program with 'mysql -p instead of 'su', but i still get the same errors.

    can somebody tell me wht these err's mean and how to debug'em.

    -sur.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The exception means that pexpect gave up waiting for the other program to send it some text. I think the default timeout is 30 seconds.

    If you run 'su' by hand you will see that it asks for a password, then puts you into a bash shell and waits for you to give it commands to execute. Since your script is not sending any commands, it does not send anything back, which is why pexpect times out.

    after sending the password, try sending the commands you want to execute as root, followed by 'exit' to get out of the root shell. I think at that point the su command will terminate and you will get the pexpect.EOF.

    Ditto for the mysql command - it expects you to send further commands, followed by 'QUIT'. It is only after you send the QUIT that you will get the EOF.


    Dave - The Developers' Coach
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    9
    Rep Power
    0

    Smile thanxs a lot ,my probs solved...


    hello coach,

    my problems solved, i m able to solve my problem of giving input to 'grid-proxy-init' using pexpect now. thanx for ur help n prompt reply, my GUI will be complete now.

    regards,
    -sur

IMN logo majestic logo threadwatch logo seochat tools logo