首页 > 解决方案 > 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()

标签: while-loopeof

解决方案


推荐阅读