簡體   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