繁体   English   中英

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

[英]Providing input to command in sub process in 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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