如何从Paramiko通道完整读取缓冲区?

发布于 2025-01-10 17:52:32 字数 835 浏览 0 评论 0原文

我正在编写一个 Python 脚本,通过 SSH 连接到我的 Raspberry Pi-Hole,并将日志数据流式传输到远程客户端。它读取良好,但会在缓冲区中的所有内容到达之前停止。我知道这一点是因为我在它旁边打开的终端运行相同的命令将显示更多条目。如果我继续浏览,那些丢失的条目将到达新数据堆栈的顶部。有什么方法可以解决这个问题吗? TIA - 第一篇文章 :) -

import paramiko
from pathlib import Path
import time

def main():
    home = str(Path.home())
    command = 'pihole -t'

    client = paramiko.SSHClient()
    client.load_system_host_keys(home + '/.ssh/known_hosts')
    client.connect(hostname='192.168.1.101', username='pi')
    transport = client.get_transport()
    channel = transport.open_session()
    channel.exec_command(command)
    while True:
        buffer = channel.recv(4096).decode('utf-8')
        print(buffer)
        time.sleep(1)



    stdin.close()
    stdout.close()
    stderr.close()
    client.close()



if __name__ == '__main__':
    main()

I am writing a Python script to SSH into my Raspberry Pi-Hole, and stream log data to a remote client. It reads out fine, but will stop before everything in the buffer arrives. I know this because the terminal I have opened right next to it running the same command will show more entries. If I continue to browse, those missing entries will arrive at the top of the stack of new data. Any ways to fix this? TIA - First Post :) -

import paramiko
from pathlib import Path
import time

def main():
    home = str(Path.home())
    command = 'pihole -t'

    client = paramiko.SSHClient()
    client.load_system_host_keys(home + '/.ssh/known_hosts')
    client.connect(hostname='192.168.1.101', username='pi')
    transport = client.get_transport()
    channel = transport.open_session()
    channel.exec_command(command)
    while True:
        buffer = channel.recv(4096).decode('utf-8')
        print(buffer)
        time.sleep(1)



    stdin.close()
    stdout.close()
    stderr.close()
    client.close()



if __name__ == '__main__':
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-01-17 17:52:32

已解决!: https://stackoverflow.com/a/53330517/18318581

一些小的修改,调用 get_pty=True 标志请求一个伪终端,并从 stdout.channel 调用数据,而不仅仅是 channel.recv。固定代码如下:

import paramiko
from pathlib import Path
import time

def main():
    home = str(Path.home())
    command = 'pihole -t'

    client = paramiko.SSHClient()
    client.load_system_host_keys(home + '/.ssh/known_hosts')
    client.connect(hostname='192.168.1.101', username='pi')
    transport = client.get_transport()
    channel = transport.open_session()
    stdout, stdin, stderr = client.exec_command(command, get_pty=True)
    while True:
        # while channel.recv_ready():
            buffer = stdout.channel.recv(4096)
            print(buffer.decode('utf-8'))
            time.sleep(1)



    client.close()



if __name__ == '__main__':
    main()

SOLVED!: https://stackoverflow.com/a/53330517/18318581

Some minor modifications, calling the get_pty=True flag to request a psuedo-terminal, and calling the data from stdout.channel instead of just channel.recv. Fixed code below:

import paramiko
from pathlib import Path
import time

def main():
    home = str(Path.home())
    command = 'pihole -t'

    client = paramiko.SSHClient()
    client.load_system_host_keys(home + '/.ssh/known_hosts')
    client.connect(hostname='192.168.1.101', username='pi')
    transport = client.get_transport()
    channel = transport.open_session()
    stdout, stdin, stderr = client.exec_command(command, get_pty=True)
    while True:
        # while channel.recv_ready():
            buffer = stdout.channel.recv(4096)
            print(buffer.decode('utf-8'))
            time.sleep(1)



    client.close()



if __name__ == '__main__':
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文