telnetlib and “buf = self.sock.recv(50)” error

为君一笑 提交于 2019-12-12 04:45:24

问题


I am using telnetlib for simple telnet script to Juniper switch. Below is my code:

import telnetlib

HOST = raw_input("Enter host IP address: ")  
USER = raw_input("Enter Username: ")  
PWD = raw_input("Enter Password: ")  
TNT = telnetlib.Telnet(HOST, 23, 10)  
TNT.read_until("login:")  
TNT.write(USER.encode('ascii') + "\n")  
TNT.read_until("Password:")  
TNT.write(PWD.encode('ascii') + "\n")  
TNT.write("set cli screen-length 10000\nconfigure\nshow\nexit\n")  
print (TNT.read_all().decode('ascii'))  
TNT.close()  
raw_input ("Press any Key to Quit: ")  

Whenever I run this program with Juniper switch it gives me this error:

Traceback (most recent call last):  
  File "D:\Python\AuTel Project\Old versions and tials\Telnet (Python 2.7) V1.4.py", line 17, in <module>  
    print (TNT.read_all().decode('ascii'))  
  File "C:\Python27\lib\telnetlib.py", line 325, in read_all  
    self.fill_rawq()  
  File "C:\Python27\lib\telnetlib.py", line 516, in fill_rawq  
    buf = self.sock.recv(50)  
timeout: timed out

I have faced this problem before with Cisco and Nortel, but I could overcome it with "terminal lenght 0" command on Cisco and similar comand on Nortel. I tried to use the equivalent command on Juniper (set cli screen-length), but I am still getting the same error. I need to know what is the meaning of this error and what is the reason of it, and how to overcome it.

Best Regards,


回答1:


The error message

buf = self.sock.recv(50)
timeout: timed out

is pretty obvious.

Your connection timed out for whatever reason.

Either some firewall or network component in between closed the connection due to inactivity after some time or the remote service did not respond within a reasonable amout of time.




回答2:


I had the same problem.

Command to change "TNT.read_all()" -> "TNT.read_some()" and script to retry.




回答3:


tn.read_very_eager()

worked very well for me. Remember we need to give sufficient sleep time before this so that it will be recorded before reading



来源:https://stackoverflow.com/questions/5528327/telnetlib-and-buf-self-sock-recv50-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!