I wrote a similar code and got similar error. Then I made the code vocal to know where I am making mistake. What I concluded is:
"Using read_all() function is not a good idea all the time. It reads infinitely and brings impression like hung mode. Try replacing it with device prompt followed by a timer during reading. And try printing it to see if code captured the desired output"
import telnetlib
import os
import sys
host = raw_input("Enter the VG IP : ")
user = "cisco"
password = "cisco"
#cmd = raw_input("Enter the command you want to feed : ")
cmd1 = "term len 0"
cmd = "show clock"
pingable = False
response = os.system("ping -c 2 " + host)
if response == 0:
pingable = True
print(host, "is Pingable", pingable)
else:
print(host, "is un-Pingable", pingable)
if(pingable):
tn = telnetlib.Telnet(host)
banner = tn.read_until("Username:", 5)
tn.write(user + "\n")
print(banner)
tn.read_until("Password:", 5)
tn.write(password1 + "\n")
prompt = tn.read_until("#")
print("I am logged in\n\n")
print(prompt)
tn.write(cmd1 + b"\n")
output1 = tn.read_until("#",5)
print("my first cmd output is :", output1, "\n")
tn.write(cmd + "\n")
output1 = tn.read_until("#",5)
print("My 2nd cmd is feeded here", output1)
tn.write("show version\n")
output1 = tn.read_until("more-- ",5)
print("version info : ", output1)
tn.write("exit\n")
else:
print(host, "is unpingable")