我有一个网络设备,我需要通过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
答案 0 :(得分:0)
从屏幕截图中可以看出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错过了哪些输出。
祝你好运调试!