【问题标题】:Looping a telnet script循环 telnet 脚本
【发布时间】:2018-04-30 15:09:08
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x automation telnet


    【解决方案1】:

    从屏幕截图看来,telnet 连接已关闭,因此循环出错。 调试此问题的最佳方法是读取每一行输出并尝试找出连接关闭的位置。

    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 中遗漏了哪些输出。

    祝调试顺利!

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2013-12-30
      • 2023-03-21
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      相关资源
      最近更新 更多