Python を使用していくつかのシェル コマンドを実行する必要がありますが、問題の 1 つを解決できませんでした。別のマシンに 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
このプログラムを実行すると、プロンプトが表示され、入力を求められます。プロンプトに自動的に応答するにはどうすればよいですか? ありがとう。