在python中的子进程中为命令提供输入



我想自动化命令行实用程序,该实用程序需要在提示下输入。我已经写了下面的程序,但它不起作用。请帮忙。

def execute_command_login(command,password,inputParameter=''):
try:
command = str(command).split(" ")
p = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin =subprocess.PIPE,bufsize=0)
#         p.stdin.write(password)
time.sleep(2)
if(p.stdout):
p.stdin.write(str(password))
if(p.stdout):
p.stdin.write(str(inputParameter))
out,err = p.communicate(password)
out = str(out).split("n")
out.remove('')
if(err):
return err
else:
return out
except:
print("Error while executing command"+str(sys.exc_info()[1]))

Pexcept模块将执行您想要的操作:https://pexpect.readthedocs.io/en/stable/index.html

相关内容

最新更新