Search code examples
pythonsshtelnetparamiko

What is ssh equivalent read_until and read_very_eager methods at telnet?


I will write a SSH communicator class on Python. I have telnet communicator class and I should use functions like at telnet. Telnet communicator have read_until and read_very_eager functions.

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).

I couldn't find these functions for SSH communicator. Any idea?


Solution

  • You didn't state it in the question, but I am assuming you are using Paramiko as per the tag.

    • read_until: Read until a given string is encountered or until timeout.

      This seems like a very specialized function for a particular high level task. I think you will need to implement this one. You can set a timeout using paramiko.Channel.settimeout and then read in a loop until you get either the string you want or a timeout exception.

    • read_very_eager: Read everything that's possible without blocking in I/O (eager).

      Paramiko doesn't directly provide this, but it does provide primitives for non-blocking I/O and you can easily put this in a loop to slurp in everything that's available on the channel. Have you tried something like this?

      channel.setblocking(True)
      resultlist = []
      while True:
          try:
              chunk = channel.recv(1024)
          except socket.timeout:
              break
          resultlist.append(chunk)
      return ''.join(resultlist)