gpt4 book ai didi

python - tail -f over ssh with Paramiko 延迟增加

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:59 26 4
gpt4 key购买 nike

我正在尝试检查正在运行的嵌入式系统的日志文件中是否有错误。

我已经在我的脚本中实现了 paramiko,因为有人告诉我这是在 python 中使用 ssh 的最佳方式。

现在,当我拖尾日志文件时,我发现有很大的延迟。每分钟增加约 30 秒。

我已经使用 grep 来减少打印的行数,因为我认为我收到了太多输入,但事实并非如此。

如何减少此延迟或阻止延迟在运行时增加。我想拖几个小时...

def mkssh_conn(addr):
"""returns an sshconnection"""
paramiko.util.logging.getLogger('paramiko').setLevel(logging.WARN)
sshcon = paramiko.SSHClient()
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshcon.connect(addr , username, password)
return sshcon

while True:
BUF_SIZE = 1024
client = mkssh_conn() #returns a paramiko.SSHClient()
transport = client.get_transport()
transport.set_keepalive(1)
channel = transport.open_session()
channel.settimeout(delta)
channel.exec_command( 'killall tail')
channel = transport.open_session()
channel.settimeout(delta)
cmd = "tail -f /log/log.log | grep -E 'error|statistics'"
channel.exec_command(cmd)
while transport.is_active():
print "transport is active"
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
buf = channel.recv(BUF_SIZE)
if len(buf) > 0:
lines_to_process = LeftOver + buf
EOL = lines_to_process.rfind("\n")
if EOL != len(lines_to_process)-1:
LeftOver = lines_to_process[EOL+1:]
lines_to_process = lines_to_process[:EOL]
else:
LeftOver = ""
for line in lines_to_process.splitlines():
if "error" in line:
report_error(line)
print line
client.close()

最佳答案

我找到了一个解决方案:似乎如果我将 BUF_SIZE 降低到 256,延迟就会减少。明显地。我需要重新检查延迟是否在运行时仍然增加。

关于python - tail -f over ssh with Paramiko 延迟增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com