如何让Python代码在telnet(telnetlib)超时后继续运行

发布于 2024-10-24 09:05:30 字数 1298 浏览 2 评论 0原文

我正在使用 Python 来使用 telnetlib 来实现自动 telnet 程序。问题是:当我尝试远程登录的设备没有响应时,意味着超时;该程序给我超时消息,并且不继续执行下一个命令。

我的代码:

import telnetlib  
HOST = ("x.x.x.x")  
USER = ("xxxxx")  
PWD = ("yyyyy")  
ENABLE = ("zzzzz")  
TNT = telnetlib.Telnet(HOST, 23, 5)                    
TNT.read_until(b"Username:")  
TNT.write(USER.encode('ascii') + b"\n")  
TNT.read_until(b"Password:")  
TNT.write(PWD.encode('ascii') + b"\n")  
TNT.write(b"enable\n")  
TNT.read_until(b"Password:")  
TNT.write(ENABLE.encode('ascii') + b"\n")  
TNT.write(b"terminal length 0\n")  
TNT.write(b"show run\n")  
TNT.write(b"exit\n")  
print (TNT.read_all().decode('ascii'))  
TNT.close()  
raw_input ("Press any Key to Quit: ")  

错误消息:

Traceback (most recent call last):  
  File "D:\Python\Telnet (Python 2.7) V1.5.py", line 8, in <module>  
    TNT = telnetlib.Telnet(HOST, 23, 5)  
  File "C:\Python27\lib\telnetlib.py", line 209, in __init__  
    self.open(host, port, timeout)  
  File "C:\Python27\lib\telnetlib.py", line 225, in open  
    self.sock = socket.create_connection((host, port), timeout)  
  File "C:\Python27\lib\socket.py", line 571, in create_connection  
    raise err  
timeout: timed out  
>>> 

如何让程序只通知我该设备无法访问并让它继续执行下一个命令?

I am using Python for Automated telnet program using telnetlib. The problem is: when the device that I am trying to telnet to doesn't responsd, means timeout; the program gives me timeout message and doesn't continue to next commands.

My Code:

import telnetlib  
HOST = ("x.x.x.x")  
USER = ("xxxxx")  
PWD = ("yyyyy")  
ENABLE = ("zzzzz")  
TNT = telnetlib.Telnet(HOST, 23, 5)                    
TNT.read_until(b"Username:")  
TNT.write(USER.encode('ascii') + b"\n")  
TNT.read_until(b"Password:")  
TNT.write(PWD.encode('ascii') + b"\n")  
TNT.write(b"enable\n")  
TNT.read_until(b"Password:")  
TNT.write(ENABLE.encode('ascii') + b"\n")  
TNT.write(b"terminal length 0\n")  
TNT.write(b"show run\n")  
TNT.write(b"exit\n")  
print (TNT.read_all().decode('ascii'))  
TNT.close()  
raw_input ("Press any Key to Quit: ")  

Error Message:

Traceback (most recent call last):  
  File "D:\Python\Telnet (Python 2.7) V1.5.py", line 8, in <module>  
    TNT = telnetlib.Telnet(HOST, 23, 5)  
  File "C:\Python27\lib\telnetlib.py", line 209, in __init__  
    self.open(host, port, timeout)  
  File "C:\Python27\lib\telnetlib.py", line 225, in open  
    self.sock = socket.create_connection((host, port), timeout)  
  File "C:\Python27\lib\socket.py", line 571, in create_connection  
    raise err  
timeout: timed out  
>>> 

How can let the program to just notify me that this device isn't reachable and let it continue with the next commands ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小帐篷 2024-10-31 09:05:30

将操作包装在 try 块中,并在 catch 块中处理异常。

Wrap the operations in a try block, and handle the exception in a catch block.

青衫负雪 2024-10-31 09:05:30

您要查找的异常是socket.timeout。所以:

import socket
try:
    TNT = telnetlib.Telnet(HOST, 23, 5)
except socket.timeout:
    sulk()

我以这种方式发现:

>>> try:
...   t = telnetlib.Telnet("google.com", 23, 5)
... except:
...   import sys
...   exc_info = sys.exc_info()
>>> exc_info
(<class 'socket.timeout'>, timeout('timed out',), <traceback object at 0xb768bf7c>)

可能是 timeout 太具体了。您可能更愿意捕获 any IOError

try:
    TNT = telnetlib.Telnet(HOST, 23, 5)
except IOError:
    sulk()

The exception you're looking for is socket.timeout. so:

import socket
try:
    TNT = telnetlib.Telnet(HOST, 23, 5)
except socket.timeout:
    sulk()

Which I discovered in this way:

>>> try:
...   t = telnetlib.Telnet("google.com", 23, 5)
... except:
...   import sys
...   exc_info = sys.exc_info()
>>> exc_info
(<class 'socket.timeout'>, timeout('timed out',), <traceback object at 0xb768bf7c>)

It might be that timeout is too specific. You might instead prefer to catch any IOError

try:
    TNT = telnetlib.Telnet(HOST, 23, 5)
except IOError:
    sulk()
仄言 2024-10-31 09:05:30

每当异常到来时,Python 就会终止你的程序。为了处理异常,您需要将其包装在 try、catch 语句中。

将 telnet 语句放在 try 语句中,并使用 except 捕获异常,如下所示:

import telnetlib  
HOST = ("x.x.x.x")  
USER = ("xxxxx")  
PWD = ("yyyyy")  
ENABLE = ("zzzzz")  
try:
    TNT = telnetlib.Telnet(HOST, 23, 5)                    
except:
    print "<your custom message>"
    pass
TNT.read_until(b"Username:")  
TNT.write(USER.encode('ascii') + b"\n")  
TNT.read_until(b"Password:")  
TNT.write(PWD.encode('ascii') + b"\n")  
TNT.write(b"enable\n")  
TNT.read_until(b"Password:")  
TNT.write(ENABLE.encode('ascii') + b"\n")  
TNT.write(b"terminal length 0\n")  
TNT.write(b"show run\n")  
TNT.write(b"exit\n")  
print (TNT.read_all().decode('ascii'))  
TNT.close()  
raw_input ("Press any Key to Quit: ")  

Python terminates your program whenever as exception arrives. For handling exception you need to wrap it in try, catch statements.

Put your telnet statement in try statement and catch exception using except as shown below:

import telnetlib  
HOST = ("x.x.x.x")  
USER = ("xxxxx")  
PWD = ("yyyyy")  
ENABLE = ("zzzzz")  
try:
    TNT = telnetlib.Telnet(HOST, 23, 5)                    
except:
    print "<your custom message>"
    pass
TNT.read_until(b"Username:")  
TNT.write(USER.encode('ascii') + b"\n")  
TNT.read_until(b"Password:")  
TNT.write(PWD.encode('ascii') + b"\n")  
TNT.write(b"enable\n")  
TNT.read_until(b"Password:")  
TNT.write(ENABLE.encode('ascii') + b"\n")  
TNT.write(b"terminal length 0\n")  
TNT.write(b"show run\n")  
TNT.write(b"exit\n")  
print (TNT.read_all().decode('ascii'))  
TNT.close()  
raw_input ("Press any Key to Quit: ")  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文