Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make python enter password when running a csh script

I'm writing a python script that executes a csh script in Solaris 10. The csh script prompts the user for the root password (which I know) but I'm not sure how to make the python script answer the prompt with the password. Is this possible? Here is what I'm using to execute the csh script:

import commands

commands.getoutput('server stop')
like image 385
darrickc Avatar asked Oct 23 '08 18:10

darrickc


3 Answers

Have a look at the pexpect module. It is designed to deal with interactive programs, which seems to be your case.

Oh, and remember that hard-encoding root's password in a shell or python script is potentially a security hole :D

like image 86
Federico A. Ramponi Avatar answered Oct 23 '22 05:10

Federico A. Ramponi


Use subprocess. Call Popen() to create your process and use communicate() to send it text. Sorry, forgot to include the PIPE..

from subprocess import Popen, PIPE

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

proc.communicate('password')

You would do better do avoid the password and try a scheme like sudo and sudoers. Pexpect, mentioned elsewhere, is not part of the standard library.

like image 5
Douglas Mayle Avatar answered Oct 23 '22 03:10

Douglas Mayle


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)

Did the trick! Pexpect rules!

like image 1
darrickc Avatar answered Oct 23 '22 05:10

darrickc