I am executing a long-running python script via ssh on a remote machine using paramiko. Works like a charm, no problems so far.
我正在使用paramiko在远程机器上通过ssh执行一个长时间运行的python脚本。工作起来很有魅力,到目前为止没有问题。
Unfortunately, the stdout (respectively the stderr) are only displayed after the script has finished! However, due to the execution time, I'd much prefer to output each new line as it is printed, not afterwards.
不幸的是,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)
How can this be achieved? Note: Of course one could pipe the output to a file and 'less' this file via another ssh session, but this is very ugly and I need a cleaner, ideally pythonic solution :)
如何实现这一点?注意:当然,可以通过另一个ssh会话将输出传输到一个文件,并“减少”这个文件,但这非常糟糕,我需要一个更干净的、理想的python解决方案:)
3 个解决方案
#1
8
As specified in the read([size]) documentation, if you don't specify a size
, it reads until EOF, that makes the script wait until the command ends before returning from read()
and printing any output.
正如read([size])文档中指定的那样,如果您没有指定大小,它将读取到EOF,这使得脚本可以等待命令结束,然后从read()返回并输出任何输出。
Check this answers: How to loop until EOF in Python? and How to do a "While not EOF" for examples on how to exhaust the File-like object.
检查以下答案:如何在Python中循环直到EOF ?以及如何执行“While not EOF”示例,说明如何耗尽类文件对象。
#2
7
I was facing a similar issue. I was able to solve it by adding get_pty=True to paramiko:
我也面临着类似的问题。我可以通过给paramiko添加get_pty=True来解决这个问题:
stdin, stdout, stderr = client.exec_command("/var/mylongscript.py", get_pty=True)
#3
4
A minimal and complete working example of how to use this answer (tested in Python 3.6.1)
如何使用这个答案的最小的和完整的工作示例(在Python 3.6.1中测试)
# run.py
from paramiko import SSHClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('...')
print('started...')
stdin, stdout, stderr = ssh.exec_command('python -m example', get_pty=True)
for line in iter(stdout.readline, ""):
print(line, end="")
print('finished.')
and
和
# example.py, at the server
import time
for x in range(10):
print(x)
time.sleep(2)
run on the local machine with
在本地机器上运行
python -m run
#1
8
As specified in the read([size]) documentation, if you don't specify a size
, it reads until EOF, that makes the script wait until the command ends before returning from read()
and printing any output.
正如read([size])文档中指定的那样,如果您没有指定大小,它将读取到EOF,这使得脚本可以等待命令结束,然后从read()返回并输出任何输出。
Check this answers: How to loop until EOF in Python? and How to do a "While not EOF" for examples on how to exhaust the File-like object.
检查以下答案:如何在Python中循环直到EOF ?以及如何执行“While not EOF”示例,说明如何耗尽类文件对象。
#2
7
I was facing a similar issue. I was able to solve it by adding get_pty=True to paramiko:
我也面临着类似的问题。我可以通过给paramiko添加get_pty=True来解决这个问题:
stdin, stdout, stderr = client.exec_command("/var/mylongscript.py", get_pty=True)
#3
4
A minimal and complete working example of how to use this answer (tested in Python 3.6.1)
如何使用这个答案的最小的和完整的工作示例(在Python 3.6.1中测试)
# run.py
from paramiko import SSHClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('...')
print('started...')
stdin, stdout, stderr = ssh.exec_command('python -m example', get_pty=True)
for line in iter(stdout.readline, ""):
print(line, end="")
print('finished.')
and
和
# example.py, at the server
import time
for x in range(10):
print(x)
time.sleep(2)
run on the local machine with
在本地机器上运行
python -m run