Reading all text in telnet connection

Eddie Corns eddie at
Thu Sep 12 12:56:36 EDT 2002

mnations at (Marc) writes:


>Having a small problem trying to capture all the text in a telnet
>connection. I've used all the options of eager, lazy, etc., and can't
>seem to find the right combo.

>What I'm doing is intermittently writing data and then reading all of
>the response. Unfortunately, all of the response might not immediately
>appear. I started off using read_until, but there's nothing really
>consistent that I can read to. So I need the ability to simply read
>everything current in the buffer until EOF.

I suspect you don't mean EOF, that only occurs when the connection is closed.
There's really only 2 ways to know when output is finished:
1) Find a string to match on
2) keep reading until enough time has passed that you're fairly sure there
   will be no more output

Because (2) is both slow and generally flaky option (1) is by far the best.
However you say that there is nothing to match on, if that's really true you
have to resort to hacks like waiting for time to pass.

Because (1) is by far the best it is worthwhile making the effort to find a
way of using it.  If the problem is that there are several alternatives that
might match you can use telnet.expect().  If there really is no sensible thing
such as a prompt to match on it may be possible to match on your own echoed
input if it's unique (this is especially true if you can send comments, I use
this in speaking to routers where the prompt keeps changing - I send #gurgle
and then look for this comimg back).

There is one more possibility but I don't think it's appropriate from your
description.  If what you are doing is sending a few pre-determined commands
collecting all the output and closing the connection then you MAY be able to
get away with sending all the commands in advance (hoping the far end buffers
them up) and use telnet.read_all() to wait for all the results (this depends
on the far end closing the connection as a consequence of the commands you

Essentialy, if there is nothing to match on you cannot reliably automate it.


More information about the Python-list mailing list