简体   繁体   English

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

[英]How to let Python code continue after telnet (telnetlib) timeout

I am using Python for Automated telnet program using telnetlib. 我正在使用telnetlib将Python用于自动telnet程序。 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 ?? 如何让程序仅通知我该设备不可访问并继续下一个命令?

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

Python terminates your program whenever as exception arrives. 每当异常到达时,Python都会终止您的程序。 For handling exception you need to wrap it in try, catch statements. 为了处理异常,您需要将其包装在try,catch语句中。

Put your telnet statement in try statement and catch exception using except as shown below: 将您的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: ")  

The exception you're looking for is socket.timeout . 您要查找的异常是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. timeout可能太具体了。 You might instead prefer to catch any IOError 您可能更喜欢捕获任何 IOError

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM