我需要使用 python 执行几个 shell 命令,但我无法解决其中一个问题。当我scp到另一台机器时,通常它会提示并询问是否将此机器添加到已知主机。我希望程序自动输入“是”,但我无法让它工作。到目前为止,我的程序如下所示:
from subprocess import Popen, PIPE, STDOUT
def auto():
user = "abc"
inst_dns = "example.com"
private_key = "sample.sem"
capFile = "/home/ubuntu/*.cap"
temp = "%s@%s:~" %(user, inst_dns)
scp_cmd = ["scp", "-i", private_key, capFile, temp]
print ( "The scp command is: %s" %" ".join(scp_cmd) )
scpExec = Popen(scp_cmd, shell=False, stdin=PIPE, stdout=PIPE)
# this is the place I tried to write "yes"
# but doesn't work
scpExec.stdin.write("yes\n")
scpExec.stdin.flush()
while True:
output = scpExec.stdout.readline()
print ("output: %s" %output)
if output == "":
break
如果我运行这个程序,它仍然会提示并要求输入。如何自动响应提示?谢谢。