我正在使用paramiko在远程机器上通过ssh执行长时间运行的python脚本.工作就像一个魅力,到目前为止没有问题.
不幸的是,stdout(分别是stderr)只在脚本完成后显示!但是,由于执行时间的原因,我更倾向于在打印时输出每个新行,而不是之后.
remote = paramiko.SSHClient()
remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote.connect("host", username="uname", password="pwd")
# myScript produces continuous output, that I want to capture as it appears
stdin, stdout, stderr = remote.exec_command("python myScript.py")
stdin.close()
for line in stdout.read().splitlines():
print(line)
Run Code Online (Sandbox Code Playgroud)
怎么能实现这一目标? 注意:当然可以通过另一个ssh会话将输出传输到文件并"减少"此文件,但这非常难看,我需要一个更清洁,理想的pythonic解决方案:)