Search code examples
pythontelnettartelnetlib

How to print out the output of "tar" execution until it finishes using telnetlib?


I tried to print out as 'tar' command extracts files, but I can't print out the output of the progress.

import telnetlib

tn = telnetlib.Telnet("x.x.x.x")

tn.write("tar -xvf a.tar")
print tn.read_until("#")
time.sleep(1800)

Above code prints out the all output after execution, and need to wait although the tar finishes less than 1800 secs.

Another try is

import telnetlib

tn = telnetlib.Telnet("x.x.x.x")

tn.write("tar -xvf a.tar")
print tn.read_eager()

print tn.read_all()

This code doesn't print the output while executing tar. Would you give me any idea how to print out while tar is running, and return as soon as it finishes tar execution without waiting?

Thank you.


Solution

  • I don't really understand what was your problem with the 1st script... Anyways can you try this?

    tn.write("tar -xvf a.tar")
    while True:
        resp = tn.read_until("#", 1.0)
        if 0 == len(resp): break
        print resp
    print "done"
    

    The loop waits for the telnet connection to become silent by specifying the timeout parameter 1.0.