March 18th, 2005, 04:16 AM
giving input to a exe called by os.system()
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.
March 18th, 2005, 05:58 AM
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:
If you are using Python2.4 then you should use the subprocess module for this instead!
os.system('grid-proxy-init %s %s' % (username, password))
Hope this helps
March 24th, 2005, 01:03 AM
problem finding material on popen()
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::
f=os.popen('c program prompting for 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.
March 24th, 2005, 01:26 AM
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 .
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 .
>>> import popen2
>>> (stdo, stdi) = popen2.popen2('sudo ls')
>>> print stdo.read()
Hope this helps,
March 24th, 2005, 02:49 AM
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 02:51 AM.
March 24th, 2005, 05:14 PM
I think that's exactly what he needs. You are always on target devcoach
I agree completly.
Originally Posted by DevCoach
April 25th, 2005, 07:08 AM
help with pexpect
i tried pexpect. but it is not working . my code is as follows::
password = getpass.getpass('Password: ')
child = pexpect.spawn("su")
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.
April 25th, 2005, 11:24 AM
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
April 26th, 2005, 08:11 AM
thanxs a lot ,my probs solved...
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.