首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环telnet脚本

循环telnet脚本
EN

Stack Overflow用户
提问于 2017-11-17 06:54:03
回答 1查看 189关注 0票数 0

我有一个网络设备,我需要通过telnet自动登录,提取特定信息,获取该信息并将其与excel电子表格进行比较。我已经成功地让python做到了这一点。当我尝试循环这个过程时,问题出现了,它在第二次循环时出错。此外,我知道必须有一种更有效的方法来做到这一点,请记住,我现在只进行了几周的编码。

代码语言:javascript
复制
import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
import sys
import os
import telnetlib
import time

usnm = "Username"
pswd = "Password"
cmd_exit = "exit"
cmd_tty = "dmstty 11"
cmd_bang = "!!!!"
cmd_logi = "logi"
cmd_pass = "0000"
cmd_logo = "logo"
cmd_que_stn = "que stn "
cmd_oamp_dn = "oamp dn"
cmd_csel = "csel none"
cmd_star = "****"
tn = telnetlib.Telnet("X.X.X.X" , "23", timeout = 5)

LG_WB = load_workbook('LEN_GWE.xlsx')
LG_SH = LG_WB.active

GWE = 0
OCM = 0

QUIT = 0
print("Loading...")
while QUIT == 0:
LEVEL_ONE = input('Type LEN, GWE, or PHONE: ')
if LEVEL_ONE == "GWE":
    LEVEL_TWO = input('GWE# ')
    for i in range(2, LG_SH.max_row+1):
        GWE_INPUT = LG_SH.cell(row=i, column=6).value
        if GWE_INPUT == int(LEVEL_TWO):
            for x in range(1, 4):
                INFO = LG_SH.cell(row=i, column=x).value
                print(INFO)
if LEVEL_ONE == "LEN":
    LEVEL_TWO = input('LEN# ')
    for i in range(2, LG_SH.max_row+1):
        LEN = LG_SH.cell(row=i, column=5).value
        if LEN == int(LEVEL_TWO):
            for x in range(1, 4):
                INFO = LG_SH.cell(row=i, column=x).value
                print(INFO)

if LEVEL_ONE == "PHONE":
    phone = input("TYPE PHONE# ")
    tn.read_until(b"user: ")
    tn.write((usnm + "\r\n").encode('ascii'))
    tn.read_until(b"password: ")
    tn.write((pswd + "\r\n").encode('ascii'))
    tn.read_until(b"$ ")
    tn.write((cmd_tty + "\r\n").encode('ascii'))
    tn.read_until(b" dmstty is starting....")
    tn.write((cmd_bang + "\r\n").encode('ascii'))
    tn.read_until(b"  ! ")
    tn.write((cmd_logi + "\r\n").encode('ascii'))
    tn.read_until(b"    PASS? ")
    tn.write((cmd_pass + "\r\n").encode('ascii'))
    tn.read_until(b"  # ")
    tn.write((cmd_csel + "\r\n").encode('ascii'))
    tn.read_until(b"  # ")
    tn.write((cmd_oamp_dn + "\r\n").encode('ascii'))
    tn.read_until(b"    REQ   ")
    tn.write((cmd_que_stn + phone + "\r\n").encode('ascii'))
    output = tn.read_until(b"    REQ   ")
    file = open("log.txt", "w")
    file.write(str(output))
    file.close()
    tn.write((cmd_star + "\r\n").encode('ascii'))
    tn.read_until(b"  # ")
    tn.write((cmd_logo + "\r\n").encode('ascii'))
    tn.read_until(b"$ ")
    tn.write((cmd_exit + "\r\n").encode('ascii'))
    tn.close()
    read_log = open("log.txt", "r")
    new_log = open("new_log.txt", "w")
    with read_log as f:
        for line in f:
            for word in line.split():
                new_log.write(word + "\n")
    read_log.close()
    new_log.close()
    read_new = open("new_log.txt", "r")
    for line in read_new:
        if "GWE" in line:
            X = read_new.read()
            GWE_LOG = open("GWE_LOG.txt", "w")
            GWE_LOG.write(X)
            GWE_LOG.close()
            READ_GWE_LOG = open("GWE_LOG.txt", "r")
            with READ_GWE_LOG as f:
                PULL_GWE = f.readline()
                GWE = GWE + int(PULL_GWE)
                READ_GWE_LOG.close()
                for i in range(2, LG_SH.max_row+1):
                    GWE_INPUT = LG_SH.cell(row=i, column=6).value
                    if GWE_INPUT == GWE:
                        for x in range(1, 4):
                            INFO = LG_SH.cell(row=i, column=x).value
                            print(INFO)
                            GWE = 0
        if "OCMA" in line:
            print("LEN GROUP IS OCMA")
            X = read_new.readlines()
            OCM_LOG = open("OCM_LOG.txt", "w")
            OCM_LOG.writelines(X[2:])
            OCM_LOG.close()
            READ_OCM_LOG = open("OCM_LOG.txt", "r")
            with READ_OCM_LOG as f:
                PULL_OCM = f.readline()
                OCM = OCM + int(PULL_OCM)
                READ_OCM_LOG.close()
                for i in range(2, LG_SH.max_row+1):
                    LEN = LG_SH.cell(row=i, column=5).value
                    if LEN == OCM:
                        for x in range(1, 4):
                            INFO = LG_SH.cell(row=i, column=x).value
                            print(INFO)
                            OCM = 0
        if "OCMB" in line:
            print("LEN GROUP IS OCMB")
            X = read_new.readlines()
            OCM_LOG = open("OCM_LOG.txt", "w")
            OCM_LOG.writelines(X[2:])
            OCM_LOG.close()
            READ_OCM_LOG = open("OCM_LOG.txt", "r")
            with READ_OCM_LOG as f:
                PULL_OCM = f.readline()
                OCM = OCM + int(PULL_OCM)
                READ_OCM_LOG.close()
                for i in range(2, LG_SH.max_row+1):
                    LEN = LG_SH.cell(row=i, column=5).value
                    if LEN == OCM:
                        for x in range(1, 4):
                            INFO = LG_SH.cell(row=i, column=x).value
                            print(INFO)
                            OCM = 0
        if "OCMC" in line:
            print("LEN GROUP IS OCMC")
            X = read_new.readlines()
            OCM_LOG = open("OCM_LOG.txt", "w")
            OCM_LOG.writelines(X[2:])
            OCM_LOG.close()
            READ_OCM_LOG = open("OCM_LOG.txt", "r")
            with READ_OCM_LOG as f:
                PULL_OCM = f.readline()
                OCM = OCM + int(PULL_OCM)
                READ_OCM_LOG.close()
                for i in range(2, LG_SH.max_row+1):
                    LEN = LG_SH.cell(row=i, column=5).value
                    if LEN == OCM:
                        for x in range(1, 4):
                            INFO = LG_SH.cell(row=i, column=x).value
                            print(INFO)
                            OCM = 0

当我尝试进行第二次搜索时,我得到了以下错误:Screenshot of error

EN

回答 1

Stack Overflow用户

发布于 2018-01-05 05:18:18

从屏幕截图可以看出,telnet连接已关闭,因此环路出现错误。对此进行调试的最佳方法是读取输出的每一行,并尝试找出连接关闭的位置。

代码语言:javascript
复制
iter=0
while True:
    iter = iter + 1
    # Check for new line
    line = telnet.read_until(('\r\n').encode('ascii'), timeout=1)
    a = (line.decode("ascii"))
    print(a)
    time.sleep(1)
    if iter == 10:
        break

在许多可疑位置使用此代码来查看您在read_until中遗漏了什么输出。

祝你调试顺利!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47340498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档