如何在CLI提示符中回答(是/否)? (paramiko远程连接)

时间:2016-04-14 21:12:18

标签: python

执行命令后如何在CLI提示符中回答(是/否)?我正在使用paramiko来执行命令。

以下是我的一段代码:

try:
    conn2 = ssh.connect(dn_name,
                        username="username",
                        password="password",
                        allow_agent=True,
                        look_for_keys=False
                        )
    if conn2 is None:
        stdin, stdout, stderr = ssh.exec_command("acidiag touch clean; reload")
        time.sleep(10)
        # Here I need to add a statement for answering YES 
        # to the above command that I gave.
        output = stdout.read()
        print output
        if output:
            ssh.close()
        else:
            stdin, stdout, stderr = ssh.exec_command("exit")

我读到了关于结构和其他模块但我无法弄清楚如何使用我的代码。

1 个答案:

答案 0 :(得分:2)

一个选项是使用yes,因此您不必担心手动说出来:

stdin, stdout, stderr = ssh.exec_command("acidiag touch clean; yes | reload")

据推测,reload命令专门要求用户输入。 yes是一个反复输出y直到被杀或其管道被破坏的程序。通过将其管道输入reload,它将自动确认每个提示。