3

I am using telnetlib to print the output after the last command i write to server

tn.write(cmd_login)
tn.write(cmd...)
tn.write(cmd_last)
print tn.expect([word],timeout)[-1]

however, when I printed the return of expectation , it also show the result I wrote to server before(eg:cmd_login cmd...) Is there anyway to only print the result after the tn.write(cmd_last) ?

newBike
  • 14,385
  • 29
  • 109
  • 192

2 Answers2

0

I would suggest using Telnet.read_until after each Telnet.write. For expected argument of read_until, telnet prompt can be used. Finally, command should be substracted from an output. I don't know better way. It could look like this:

tn.write(cmd_login)
tn.read_until(prompt)
tn.write(cmd...)
tn.read_until(prompt)
tn.write(cmd_last)
output = tn.read_until(prompt)
cmdIndex = output.find(cmd_last)
output = output[cmdIndex + len(cmd_last):]
print output
Marcin Kowalczyk
  • 649
  • 6
  • 17
0

I solved it by running a read_very_lazy() + read_until:

tn.read_very_lazy()
tn.write(b'the_command\r\n')
tn.read_until(b'\r\n', timeout=2)

However, I didn't test it very well yet to claim that it doesn't have any negative side effect.

freeAR
  • 943
  • 3
  • 18
  • 32