telnetlib python read_all() not working(hangs)

fsociety picture fsociety · Oct 10, 2014 · Viewed 8.5k times · Source

I am trying to read from a cisco router using telnetlib

import telnetlib
tn = telnetlib.Telnet(’10.106.218.50’, 17280)
cmd1=”enable”
cmd2=”show run”
#session.write("command".encode('ascii') + b"\r")
tn.write(cmd1.encode('ascii') + b"\r")
tn.write(cmd2.encode('ascii') + b"\r")
#op=tn.read_very_eager()
#op=tn.read_some()
#op=tn.read_until('#')
op=tn.read_all()
print op

I am able to write to the console of the router successfully However the system just hangs,when i try to read from the console of the router. When i use read_some(), i get a part of the output.But read_all() just hangs and gives no response Please suggest a solution

Answer

Jiynx picture Jiynx · May 6, 2015

the

read_all()

command in python's telnetlib module will block if there's no timeout specified when you make your connection.

your invocation command should look like

tn = telnetlib.Telnet('10.106.218.50', 17280, timeout = 1)

you can also substitute your own timeout value.