telnetlib 和“buf = self.sock.recv(50)”错误
我使用 telnetlib 作为到 Juniper 交换机的简单 telnet 脚本。下面是我的代码:
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: ")
每当我使用 Juniper 交换机运行此程序时,都会出现此错误:
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
我之前在 Cisco 和 Nortel 上遇到过此问题,但我可以在 Cisco 上使用“terminal lenght 0”命令以及在 Nortel 上使用类似的命令来克服它。我尝试在 Juniper 上使用等效命令(设置 cli screen-length),但仍然遇到相同的错误。我需要知道这个错误的含义是什么,其原因是什么,以及如何克服它。
此致,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
错误信息
非常明显。
无论出于何种原因,您的连接超时了。
由于一段时间后不活动,或者远程服务在合理的时间内没有响应,中间的某些防火墙或网络组件关闭了连接。
The error message
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.
我也有同样的问题。
更改命令“TNT.read_all()”-> “TNT.read_some()”
和重试脚本。
I had the same problem.
Command to change "TNT.read_all()" -> "TNT.read_some()"
and script to retry.
尝试完全退出终端,我遇到了类似的问题,退出脚本后出现错误消息。问题是,我正在启用模式而不是从终端退出。一旦我添加
Exit
作为退出 telnet 脚本的最后一个命令,它就起作用了。这是我在输入
ctr+c
后遇到的错误从脚本中截取:
Try to exit from terminal completely, I had similar issue same, error message after to quit script. The issue was, I was getting to enable mode and not exiting from terminal. Once I added
Exit
as last command to exit telnet script, it worked.This was error I was getting, after I keyed
ctr+c
Snipped from script:
这对我来说非常有效。
请记住,在此之前我们需要提供充足的睡眠时间,以便在阅读之前进行记录。
This worked very well for me.
Remember, we need to provide sufficient sleep time before this, so that it will be recorded before reading.