Vinod HC Vinod HC - 3 years ago 376
Python Question

Reading data using tn.read_all() in python

read_all()" to read data from a cisco device.
some time it reads the data and sometime it won't read and gives empty string.
I tried below 2 commands but still it's not consitently reading data.

data=tn.read_until("exit")
data= tn.read_all()


please give some inputs i am new to python.

code i am using:

_command2='show chassis'
print 'Commands issued............'
#ISSUE COMMANDS VIA TELNET
tn.write("term len 0\r")
#tn.read_until(" ")
#tn.write(_command1+"\r")
tn.write(_command2+"\r")
tn.write("exit\r" )
print 'Read telnet data............'
#READ TELNET DATA
#data=tn.read_eager()
data=tn.read_until("exit")
#data= tn.read_all()
#print data
print 'Telnet data read successfully............'

Answer Source

I too faced the same problem..This would help:

tn = telnetlib.Telnet('64.0.0.1')
tn.write('ls \r\n')
data = '' 
while data.find('#') == -1:
    data = tn.read_very_eager()
print data

This snippet reads the info after a command being executed. And reads till '#' prompt is shown.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download