0

I'm using Python's telnetlib to telnet to network router and executing few commands and I want to get the output of these commands.

tn   =   telnetlib . Telnet ( "x.x.x.x", 23, 600 ) 
tn.expect([b"login as:"], 5)
tn.write(b'admin\r')
tn.expect([b"Password:"], 5)
tn.write(b"abc\r")
tn.write(b"show version\r")
time.sleep(3)
ret1 = tn.read_eager().decode('ascii')
print("Show Version Start \n")
print(repr(ret1))
print("\nShow Version End")
tn.write(b"exit\r")
print(tn.read_all().decode('ascii'))
tn.close()

I only want output of show version but its showing all output, is there way to only get output of last command executed??

ImranRazaKhan
  • 1,955
  • 5
  • 33
  • 74

1 Answers1

1

Funny thing, I am also doing same work and I have found solution to this, check this link https://coderwall.com/p/0uxfba/read-stdout-from-telnet-terminal-in-python.

If you empty your_class.data before calling getMoreData, you will always have your last command output saved into data list and you can print it out than.

Hope it helps

Max Zix
  • 55
  • 1
  • 8