我将在 Python 上编写一个 SSH 通信器类。我有 telnet 通信器类,我应该使用类似 at telnet 的功能。 Telnet通信器有read_until和read_very_eager函数。
read_until : Read until a given string is encountered or until timeout.
read_very_eager : Read everything that's possible without blocking in I/O (eager).
我找不到 SSH 通信器的这些功能。有什么想法吗?
最佳答案
您没有在问题中说明,但我假设您正在使用 Paramiko根据标签。
read_until
:读取直到遇到给定字符串或直到超时。这似乎是针对特定高级任务的非常专业的函数。我认为您需要实现这一计划。您可以使用 paramiko.Channel.settimeout 设置超时然后循环读取,直到得到所需的字符串或超时异常。
read_very_eager
:在不阻塞 I/O 的情况下读取所有可能的内容(eager)。Paramiko 并不直接提供此功能,但它确实提供了非阻塞 I/O 的原语,您可以轻松地将其放入循环中以获取 channel 上可用的所有内容。你尝试过这样的事情吗?
channel.setblocking(True) resultlist = [] while True: try: chunk = channel.recv(1024) except socket.timeout: break resultlist.append(chunk) return ''.join(resultlist)
关于python - telnet 上 ssh 等效的 read_until 和 read_very_eager 方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9921115/