Question

I am trying to automate some telnet actions with python3. So I started to use the build in telnetlib (not pexpect).

Actually everything works so far but I do not understand completly how read_until works - actually the documentation says that you can set a timeout and if the search string is not found the timeout sends back a empty byte value or or if there is another value thats the one which can be stored as a return value!

Does that make sense ?!

If I wanna read_until a certain value - how do I find out that this value was really the one the function read. Also I couldn't find out how to check if the timeout was hit.

My workaround for now is:

output = telnet.read_until(str.encode(hostname), 3)
if re.search(hostname, bytes.decode(output), re.IGNORECASE):
   #do something when the output matches the searchstring
else:
   #stop the function

but that doesn't make any sense for me, so perhaps you now a better solution

Was it helpful?

Solution

Yes, if your hostname is example.com, read_until will return something like weijwrgnerg hgqwv blather example.com. Except if it hasn't found the hostname after three seconds, it will just spit out whatever it's got by then: weijwrgnerg hg.

So I think you're doing it almost right. You probably want to re.escape your hostname, otherwise it will interpret it as a regex. Alternatively, you could just use Python's .endswith(). And you can do output.decode() to get a string (rather than bytes.decode(output)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top