while-loop - SyntaxError:解析时出现意外的 EOF - tn.close()
问题描述
需要为远程登录到设备、运行命令并退出远程登录的工作编写一些代码
我不断收到错误
文件“python”,第 54 行 tn.close() ^ SyntaxError:解析时出现意外 EOF
我做错了什么,编码新手
干杯
import time
import telnetlib
tn_username = "xxxx"
tn_password = "xxxxxxx"
#Globals:
CACHE_DATA = {}
SNIPPET_NAME = 'xxxxxx: xxxxxx'
FAILED_COUNT = 0
COLLECTION_PROBLEM = False
TELNET_PORT = 23
TELNET_TIMEOUT = 2
FAILED_ITEMS = []
self.logger.ui_debug('************** %s: Starting *******************' % (SNIPPET_NAME))
#start timer
start_time = time.time()
try:
#connect to telnet
tn = telnetlib.Telnet
tn.read_until("login: ")
tn.write(tn_username + "\n")
tn.read_until("Password: ")
tn.write(tn_password + "\n")
for obj_oid in result_handler.oids:
##obj_name = result_handler[obj_oid]['name']
try:
#run oid as CLI call from result_handler
tn.write(obj_oid+"\r")
rawdata = tn.read_until("Welcome to the Tesira Text Protocol Server...", TELNET_TIMEOUT)
if rawdata:
result_handler[obj_oid] = [(0,"Collection Ok")]
CACHE_DATA[obj_oid] = rawdata.strip()
else:
FAILED_COUNT += 1
result_handler[obj_oid] = [(0,"Failed: No data found")]
FAILED_ITEMS.append(obj_oid)
except:
FAILED_ITEMS.append(obj_oid)
result_handler[obj_oid] = [(0,'Failed: Collection: %s' % obj_oid)]
FAILED_COUNT +=1
#save job time for perf graph
CACHE_DATA['biamp'] = round(time.time() - start_time,4)
#gracefully quit the telnet session so as to not leave any defunct processes on the host device.
tn.write("bye\r")
tn.close()
解决方案
推荐阅读
- r - 在每组数据集中找到重叠时间
- installanywhere - 超过许可的最大允许同时构建
- sql - 如何仅向查询结果中出现的用户发送电子邮件?
- javascript - 如何检测在 Javascript keyup 事件中何时按下 Shift+NumKey?
- c# - 在列表中使用 linq 和 c# 进行分组
- tensorflow - 无法在 Ubuntu 18.04 上使用 apt 安装 TensorFlow - 未满足对 nvidia-driver-418 的依赖
- asp.net - 在循环中执行存储过程是否有更有效的替代方法?
- unity3d - 在 Oculus Quest build 7.0 上无法同时对两个控制器进行跟踪
- python - 如何将给定的字符串转换为数组?
- python - 我们在 OpenCV 和 Python 中有椭圆检测吗?