前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >随机命令自动测试脚本 | SecureCRT 下 Python 实现

随机命令自动测试脚本 | SecureCRT 下 Python 实现

作者头像
宋天伦
发布2020-11-19 14:07:06
9140
发布2020-11-19 14:07:06
举报
文章被收录于专栏:fryteafrytea

在进行命令终端软件开发及通信道路开发时,常常需要测试终端的稳定性,此时通过命令脚本自动化的敲命令可以节省许多人力,这里写了一个SecureCRT终端模拟软件下python实现的自动化测试脚本,可以从命令列表中随笔选取命令进行测试,具有设置睡眠时间及超时发现功能。

代码语言:javascript
复制
# $language = "python"
# $interface = "1.0"

# This automatically generated script may need to be
# edited in order to work correctly.

import time
import math
import random

start_time = time.time()
end_time = time.time()
cmd = "show sw"
cmd_list = [
    'show interface switchport xge 1/0/20',
    'show vlan all',
    'show arp all',
    'show mac-address all',
    'show trap message filter level 1'
]
cmd_list_len = len(cmd_list) - 1
time_sleep = 0.1
time_out = 30

def Main():
    crt.Screen.Send("co te\r\n")
    crt.Screen.Send("int xge 1/0/6\r\n")
    while True:
        end_time = time.time()
        crt.Screen.Synchronous = False
        #crt.Screen.Send(cmd)
        CmdNum = random.randint(0,cmd_list_len)
        crt.Screen.Send(cmd_list[CmdNum])
        crt.Screen.Send("\r\n")
        running_time = end_time - start_time
        running_min = math.ceil(running_time / 60) - 1
        if (crt.Screen.WaitForCursor(time_out)):
            #crt.Screen.Send("\r\n")
            #crt.Screen.Send("\r\n")
            #crt.Screen.Send("\r\n")
            #crt.Screen.Send("\r\n")
            #crt.Screen.Send("\r\n")
            if CmdNum == 10:
                time.sleep(10)
            else:
                time.sleep(time_sleep)
            #time.sleep(100 / (running_time / 1000))
        else:        
            msg = "超时无响应,运行报告:\n" + \
                "running time : " + str( running_min ) + " min " + str(running_time - running_min * 60 - time_out) + " s "  + "\n" + \
                "start ms        : " + str(start_time)             + "\n" + \
                "ent ms          : " + str(end_time)               + "\n"
            crt.Dialog.MessageBox(msg,"session",64|2)

Main()

Author: Frytea

Title: 随机命令自动测试脚本 | SecureCRT 下 Python 实现

Link: https://cloud.tencent.com/developer/article/1748963

Copyright: This work by TL-Song is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档