【发布时间】: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