telnetlib 和“buf = self.sock.recv(50)”错误

发布于 2024-10-30 04:02:17 字数 1222 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小矜持 2024-11-06 04:02:17

错误信息

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

非常明显。

无论出于何种原因,您的连接超时了。

由于一段时间后不活动,或者远程服务在合理的时间内没有响应,中间的某些防火墙或网络组件关闭了连接。

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.

心不设防 2024-11-06 04:02:17

我也有同样的问题。

更改命令“TNT.read_all()”-> “TNT.read_some()”
和重试脚本。

I had the same problem.

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

别挽留 2024-11-06 04:02:17

尝试完全退出终端,我遇到了类似的问题,退出脚本后出现错误消息。问题是,我正在启用模式而不是从终端退出。一旦我添加 Exit 作为退出 telnet 脚本的最后一个命令,它就起作用了。
这是我在输入 ctr+c 后遇到的错误

^CTraceback (most recent call last):
  File "./python_ex1_telnet_reading_file_1.py", line 44, in <module>
    tn.write(b"exit\n")
  File "/usr/lib/python3.8/telnetlib.py", line 335, in read_all
    self.fill_rawq()
  File "/usr/lib/python3.8/telnetlib.py", line 526, in fill_rawq
    buf = self.sock.recv(50)

从脚本中截取:

tn.write(b"end\n")
tn.write(b"exit\n") -- This was missing
print(tn.read_all().decode('ascii'))

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

^CTraceback (most recent call last):
  File "./python_ex1_telnet_reading_file_1.py", line 44, in <module>
    tn.write(b"exit\n")
  File "/usr/lib/python3.8/telnetlib.py", line 335, in read_all
    self.fill_rawq()
  File "/usr/lib/python3.8/telnetlib.py", line 526, in fill_rawq
    buf = self.sock.recv(50)

Snipped from script:

tn.write(b"end\n")
tn.write(b"exit\n") -- This was missing
print(tn.read_all().decode('ascii'))
小瓶盖 2024-11-06 04:02:17

这对我来说非常有效。

tn.read_very_eager()

请记住,在此之前我们需要提供充足的睡眠时间,以便在阅读之前进行记录。

This worked very well for me.

tn.read_very_eager()

Remember, we need to provide sufficient sleep time before this, so that it will be recorded before reading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文