Search code examples
pythontelnetlib

Restrict or limit read_very_eager() in python


I am executing a few commands on a remote machine which runs linux over a telnet session using telnet lib. When I give the following :

tn.write("<binary_name>")
time.sleep(10)
tn.write("cat <path_of_output_file>")
rd_buf=tn.read_very_eager()
print(rd_buf)

it gives the output of the binary and the output of cat command. While I only want the output of the cat command. I have only worked with read_very_eager and read_until. How do I restrict my buffer variable to hold only the contents of the cat command?


Solution

  • After a bit of playing around with read_until() and read_very_eager(), I finally solved this. All I had to do was this :

    tn.write("<binary_name>")
    time.sleep(10)
    junk=tn.read_very_eager()
    tn.write("cat <path_of_output_file>")
    rd_buf=tn.read_very_eager()
    print(rd_buf)
    

    With the addition of 1 line (junk=tn.read_very_eager()), I am able to achieve what I wanted. Quoting the python official documentation :

    Telnet.read_very_eager()

    Read everything that can be without blocking in I/O (eager).

    I took a cue from this and made the script forcefully halt the "reading". And begin reading all over again, which served the purpose.