I am writing a python script that runs a csh script in Solaris 10. The csh script asks the user for the root password (which I know), but I'm not sure how to make the python script respond to the password request. Is it possible? Here is what I use to execute the csh script:
import commands commands.getoutput('server stop')
Take a look at the pexpect module . It is designed to work with interactive programs, which, apparently, in your case.
Oh, and remember that the root root encoding password in a shell or python script could potentially be a security hole: D
subprocess. Popen(), () . , PIPE..
from subprocess import Popen, PIPE proc = Popen(['server', 'stop'], stdin=PIPE) proc.communicate('password')
, sudo sudoers. Pexpect, , .
import pexpect child = pexpect.spawn('server stop') child.expect_exact('Password:') child.sendline('password') print "Stopping the servers..." index = child.expect_exact(['Server processes successfully stopped.', 'Server is not running...'], 60) child.expect(pexpect.EOF)
! Pexpect!
. - :
commands.getoutput('server stop -p password')
:
import popen2 (stdout, stdin) = popen2.popen2('server stop') stdin.write("password")
100%. , "" , su: , csh script, su root.
In order not to answer the "Password" question in a python script, I just run the script as root. This question is still unanswered, but I think I will just do it now.
Add input=in proc.communicate()to make it for guys who like to use the standard library.
input=
proc.communicate()
from subprocess import Popen, PIPE proc = Popen(['server', 'stop'], stdin=PIPE) proc.communicate(input='password')