How to send the password after user name in comman

2019-06-03 18:00发布

问题:

Trying to run some windows application in a specific user mode. After passing the command, it will ask for password. So passing the password using proc.communicate() but its not working, Please help

from subprocess import Popen, PIPE
import time
cmd = "runas /user:administrator notepad.exe"
proc = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=PIPE)
print proc.stdout.read()
proc.communicate('password')

回答1:

Are you open to using Pexpect instead? If yes, you can use the following:

import pexpect
cmd = "runas /user:administrator notepad.exe"
child_process = pexpect.spawn(cmd)
child_process.expect('assword')
child_process.sendline(password)