Make python enter password when running csh script

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')
+5
source share
7 answers

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

+7

subprocess. Popen(), () . , PIPE..

from subprocess import Popen, PIPE

proc = Popen(['server', 'stop'], stdin=PIPE)

proc.communicate('password')

, ​​ sudo sudoers. Pexpect, , .

+3
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!

+1

. - :

commands.getoutput('server stop -p password')
0

:

import popen2

(stdout, stdin) = popen2.popen2('server stop')

stdin.write("password")

100%. , "" , su: , csh script, su root.

0

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.

0
source

Add input=in proc.communicate()to make it for guys who like to use the standard library.

from subprocess import Popen, PIPE
proc = Popen(['server', 'stop'], stdin=PIPE)
proc.communicate(input='password')
0
source

All Articles