1

I have the following code ,line "call("ectool --server=commander.company.com login username",shell=True)" prompts for a password,how do I pass the password to the shell?

from subprocess import call

def main():
    list = ['741679','741477']
    call("export COMMANDER_SERVER=commander.company.com" ,shell=True)
    call("ectool --server=commander.company.com login username",shell=True)
    #need to pass password

if __name__ == '__main__':
    main()
python.beginner
  • 365
  • 3
  • 6
  • 12

1 Answers1

0

You might want to check out the expect library for python. This library allows you to write a "script" which specifies how your program should respond to the subprocess, based on prompts.

You should be able to do something like this:

import pexpect
child = pexpect.spawn ('ectool --server=commander.company.com login username')
child.expect('Password:')
child.sendline('<supersecretpassword>')

note: credit where it's due - https://stackoverflow.com/questions/2387731/use-subprocess-to-send-a-password

Community
  • 1
  • 1
metasoarous
  • 2,854
  • 1
  • 23
  • 24