H3C_线路测试脚本

之前做了一个割接网络的项目,在过程中要测试网点和中心的几百条专线线路,心想要逐个 ping 测试个对端IP地址,在时间上,操作上根本不可行,整个割接时间有限定,又是在凌晨时间……限制比较多,刚好那段时间在玩 Python,就想着能不能做个脚本自己跑,最后实现了,而且生成 Excel 表格统计测试成功和测试失败的信息。

操作

记录会话日志(log session)

  • 设置会话属性参数(可单独设置串口会话、telnet 会话和 ssh 会话,右键会话属性修改即可)

  • 设置 log file 参数(设置保存路径,选项,日志的开头和结束位置插入信息(脚本用于判断结束的位置))

制作端口和 IP 信息的 txt 文件

说明:第一列为网络中心路由器接口,第二列为对端网点设备的 IP 地址(接口和IP我已经处理过了,假的,真的不会贴出来)

编写 ping 测试 Python 脚本

  • 脚本内容
# $language = "python"
# $interface = "1.0"
import os
import SecureCRT


def main():
    crt.Screen.Synchronous = True
    with open('F:/SR66_Test/SR66.txt', 'r') as rfile:
        ping_list = rfile.readlines()
        # 遍历SR66文件里的每一行,把第1列赋值给 port_info,第二列赋值给 ping_ip
        for ping_info in ping_list:
            port_info = ping_info.split()[0]
            ping_ip = ping_info.split()[1]
            crt.Screen.Send('\r')
            # 在路由器命令行界面输入#+port_info并回车,例如:#GE4/1/0.111
            crt.Screen.Send('#' + port_info + '\r')
            # 在路由器命令行界面输入ping -c + ping_ip并回车,例如:ping -c 3 100.10.142.30
            crt.Screen.Send('ping -c 3 ' + ping_ip + '\r')


main()
  • SceureCRT 登陆路由器,点击 “Script” –> “Run” 选择脚本,运行脚本。运行时间取决于线路的正常连通和测试的数量

编写结果统计 Python 脚本

  1. 处理 ping 测试后的 log session 日志记录
  2. 提取 ping 测试的结果信息(会同时生成一个 result.txt 的文件)
  3. 对 result.txt 文件的信息进行处理生成 Excel 表格
  • 脚本内容:
# Author:KIRASTER
# Time:2021/x/x x:x
# Description:
import os
import re
import xlsxwriter as xw
from xlsxwriter import Workbook

filename = 'F:/SR66_Test/xxx.log'
resultfilename = 'F:/SR66_Test/result.txt'
if os.path.exists(resultfilename):
    os.remove(resultfilename)
result_txt = open('F:/SR66_Test/result.txt', 'a')

with open(filename, 'r', encoding='utf8') as file_to_read:
    while True:
        lines = file_to_read.readline()
        if "GE" in lines:
            #  # 提取GE开头的行,也就是端口和IP信息的txt文件里的第一列,并且换行符替换制表符
            result_line1 = re.sub(r'^.*?#_', '', lines)
            result_line1 = result_line1.replace("\n", "\t")
            print(result_line1)
            result_txt.write(result_line1)
        if "--- Ping statistics" in lines:
            # 提取包含“--- Ping statistics”这一行,替换成“ping + IP”,并且换行符替换制表符
            result_line2 = re.sub(r'^.*?for', 'ping', lines)
            result_line2 = result_line2.replace("-", "")
            result_line2 = result_line2.replace("\n", "\t")
            print(result_line2)
            result_txt.write(result_line2)
            lines = next(file_to_read)
            # 提取下一行,替换成“result: 0.0% packet loss”
            result_line3 = re.sub(r'^.*?received,', 'result:', lines)
            print(result_line3)
            result_txt.write(result_line3)
            result_txt.flush()
        if "结束记录时间" in lines:
            # 遍历到“结束记录时间”这行,中断循环
            break

workbook: Workbook = xw.Workbook('F:/SR66_Test/result.xlsx')

sheet0 = workbook.add_worksheet('测试结果')
centered = workbook.add_format({'align': 'center'})
red_style = workbook.add_format({
    "fg_color": "red",'align': 'center','bold': True})
green_style = workbook.add_format({
    "fg_color": "green",'align': 'center','bold': True})
blue_style = workbook.add_format({
    "fg_color": "#8DB4E2",'align': 'center'})
sheet0.write(0, 0, '检测端口', blue_style)
sheet0.write(0, 1, '对端IP地址', blue_style)
sheet0.write(0, 2, '返回结果', blue_style)
sheet0.write(0, 3, '检测结果', blue_style)
sheet0.write(0, 4, '线路正常', green_style)
# sheet0.write(0, 5, '', green_style)
sheet0.write(0, 6, '检测失败', red_style)
# sheet0.write(0, 7, '', red_style)
sheet0.set_column(0, 0, width=20)
sheet0.set_column(1, 1, width=24)
sheet0.set_column(2, 2, width=28)
sheet0.set_column(3, 3, width=12.38)
sheet0.set_column(4, 4, width=12.38)
sheet0.set_column(5, 5, width=5)
sheet0.set_column(6, 6, width=12.38)
sheet0.set_column(7, 7, width=5)
sheet0.freeze_panes(1, 8, top_row=1)
# sheet0.filter_column_list("D", ['正常', '检测失败'])
sheet0.filter_column_list(3, 'x = "正常"')
sheet0.write_formula('D2', '{=IF(ISNUMBER(FIND("100.0%",C2)),"检测失败",IF(ISNUMBER(FIND("0.0%",C2)),"正常"))}')
sheet0.write_formula('F1', '{=SUMPRODUCT((D2:D999="正常")*1)}')
sheet0.write_formula('H1', '{=SUMPRODUCT((D2:D999="检测失败")*1)}')

with open('D:/SR6608_Test/result.txt') as file_object:
    lines = file_object.readlines()
    file_object.close()

i = 1
x = 1
for xls_line in lines:
    if "GE" in xls_line:
        xls_line = xls_line.strip('\n')
        xls_line = xls_line.split('\t')
        print(xls_line)
        port_info = xls_line[0]
        ip_addr = xls_line[1]
        return_info = xls_line[2]
        sheet0.write(i, 0, port_info, )
        sheet0.write(i, 1, ip_addr, )
        sheet0.write(i, 2, return_info, )
        # sheet0.write_formula(x, 3, '=IF(ISNUMBER(FIND("100.0%",C2)),"正常",IF(ISNUMBER(FIND("0.0%",C2)),"检测失败"))')
        i += 1
        x += 1

workbook.close()
  • ping 测试的 log session 文件

  • 生成的 result.txt 文件

  • 生成的 Exce 表格