简体   繁体   中英

pexpect to pass password to a script

I'm trying to run a code which will trigger a script. The script evaluates something and uploads the results to a site for which it prompts password from user. Need to automate this with a generic user.

I have

import subprocess
p=subprocess.Popen(cmd,cwd=path)
p.wait()

When this runs, it evaluates something and prompts for password. How to give this password using pexpect? Is there any other solution to it?

By using the spawn class

p = ['cmd','to','run']
child = pexpect.spawn(' '.join(p), cwd='/path/to/dir')
# Now you give a regex of the expected password prompt
# usually use 'pass.*'
# but for a general case use '.*'
child.expect('.*')
child.sendline('abc123')  # your secure password
child.wait()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM