首页 > 其他分享 >如何使用自动化构造随机路由模型

如何使用自动化构造随机路由模型

时间:2023-07-18 16:45:51浏览次数:30  
标签:edit list community api 随机 BgpRoute 自动化 路由

为什么要仿真随机路由?

路由器测试中,为了最大程度还原现网路由情况,评估路由器在现网环境下稳定工作各项指标,需要对导入路由进行离散仿真,目前路由仿真可分为导入路由与生成路由两种方式,导入路由需要现网路由表导入,本文讨论重点为生成路由方式。

自动化生成路由能解决什么问题?

使用用户界面生成路由时,可根据离散模型生成路由,但生成路由与现网路由相比,只注重路由段离散,未体现AsPath、Community等BGP路由参数离散,使用自动化生成路由可根据定义规则进行生成。

如何使用自动化生成随机路由

信而泰Renix平台提供了python API接口,可使用python API进行路由灵活定义。假设路由需求如下:配置Port口1个,包含20个IBGP,个IBGP通告10个路由段、共10wIPv4+10wIPv6路由,要求路由掩码随机选择,AsPath随机选择、Connmity随机选择。

本文选择基础API使用信而泰API(renix_py_api、MiscLibrary),不另做定义,使用时需安装相关环境。代码解析如下:

导入相关库

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import time

from renix_py_api.renix import *

from MiscLibrary.base import *

import logging

import random

import re

初始化Python环境及定义参数

if __name__ == '__main__':

# 初始化环境

print('######################初始化环境######################')

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

###############################################################

api = MiscAPI()

initialize(log=True, log_level=logging.INFO, log_handle=LogHandle.LOG_FILE)

###############################################################

# 占用测试仪端口

chassis_DY = "10.1.1.7"

port_DY_1 = "//10.1.1.7/3/1"

# 路由起始地址

start_ip1 = "20.0.0.0"

start_ipv61 = "2023::"

#bgp路由参数

RgpSessionCount = 20

BgpRouteBlock = 10

BgpRouteBlockv6 = 10

ipv4routecount = 100000

ipv6routecount = 100000

#ipv4路由掩码

MaskMin = 20

MaskMax = 30

# ipv6路由掩码

PrefixMin = 80

PrefixMax = 120

# bgp as_path&community限制

AsPathMaxLength = 8

CommunityMaxLength = 8

###############################################################

#其它参数

Ipv4RoutePerSession = int(ipv4routecount / RgpSessionCount)

ipv6PrefixPerSession = int(ipv6routecount / RgpSessionCount)

Ipv4CountRandonMax = int(Ipv4RoutePerSession / BgpRouteBlock)

Ipv4CountRandonMin = int(Ipv4CountRandonMax * 0.5)

Ipv6CountRandonMax = int(ipv6PrefixPerSession / BgpRouteBlockv6)

Ipv6CountRandonMin = int(Ipv6CountRandonMax * 0.5)

print('######################初始化完成######################')

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

创建端口及映射机箱

print('#######################连接机箱#######################')

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

sys_entry = get_sys_entry()

sys_entry.edit(ProductType=1)

chassis = ConnectChassisCommand(chassis_DY)

# chassis.execute()

# 占用端口、端口上线

port_1 = Port(upper=sys_entry, Location=port_DY_1, name='port_1')

# BringPortsOnlineCommand(PortList=[port_1.handle,port_2.handle]).execute()

print('######################连接成功!######################')

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

print('#######################创建IBGP#######################')

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

# 参数生成器定义

GeneratorRouteridv4 = api.address_modifier(Start=r'192.168.0.1', Step=1, Count=1000, Offset=0)

GeneratorRouteridv6 = api.address_modifier(Start=r'192:168::1', Step=1, Count=1000, Offset=0)

GeneratorMacAddress = api.address_modifier(Start=r'00:10:94:00:00:01', Step=1, Count=1000, Offset=0)

GeneratorIPv4Address = api.address_modifier(Start=r'10.0.0.2', Step=1, Count=1000, Offset=8)

GeneratorIPv6Address = api.address_modifier(Start=r'2000::2', Step=1, Count=1000, Offset=80)

#接口生成interface

for x in range(RgpSessionCount):

#接口参数定义

Routeridv4 = api.generator_next(GeneratorRouteridv4)

Routeridv6 = api.generator_next(GeneratorRouteridv6)

MacAddr = api.generator_next(GeneratorMacAddress)

IPv4Addr = api.generator_next(GeneratorIPv4Address)

IPv6Addr = api.generator_next(GeneratorIPv6Address)

IPv4GWAddr = api.ipv4_address_hopping(IPv4Addr, Mask=32, Type='decrement', Step=1)

IPv6GWAddr = api.ipv6_address_hopping(IPv6Addr, Mask=128, Type='decrement', Step=1)

# 创建IPv4接口

interface = Interface(upper=port_1, RouterId = Routeridv4, Ipv6RouterId = Routeridv6)

Interface_temp = "Interface_" + str(x+1)

build_Dual = BuildInterfaceCommand(InterfaceList=Interface_temp, NetworkLayers=['eth', 'vlan'], TopLayers=['ipv4', 'ipv6'])

build_Dual.execute()

eth_layer = interface.get_children('EthIILayer')[0]

eth_layer.edit(Address = MacAddr)

vlan_layer = interface.get_children('VlanLayer')[0]

vlan_layer.edit(VlanId = x+1 )

ipv4_layer = interface.get_children('Ipv4Layer')[0]

ipv4_layer.edit(Address = IPv4Addr , Gateway=IPv4GWAddr)

ipv6_layer = interface.get_children('Ipv6Layer')[0]

ipv6_layer.edit(Address = IPv6Addr , Gateway=IPv6GWAddr)

创建BGP协议及路由

# 创建BGP协议

BgpSession = BgpProtocolConfig(upper=port_1)

BgpSession.edit(AsNumber=65000)

BgpSession.edit(DutAsNumber=65000)

BgpSession.edit(UseGatewayAsDutIp=False)

BgpSession.edit(DutIpv4Address=IPv4GWAddr)

BgpSession.edit(DutIpv6Address=IPv6GWAddr)

# 绑定Dual接口和协议

select_interface = SelectInterfaceCommand(ProtocolList=[BgpSession.handle], InterfaceList=[interface.handle])

select_interface.execute()

# IPv4路由block创建

FirstRoute = start_ip1

Ipv4RouteCount = 0

for y in range(BgpRouteBlock):

mask = random.randint(MaskMin, MaskMax)

if y == BgpRouteBlockv6-1:

RouteCount = Ipv4RoutePerSession - Ipv4RouteCount

else:

RouteCount = random.randint(Ipv4CountRandonMin, Ipv4CountRandonMax)

Ipv4RouteCount = Ipv4RouteCount+RouteCount

offset = 32 - mask

if x == 0 and y == 0:

# bgp参数修改

BgpRoute = BgpIpv4RoutepoolConfig(upper=BgpSession)

BgpRoute.get()

BgpRoute.edit(FirstRoute=FirstRoute)

BgpRoute.edit(PrefixLength=mask)

BgpRoute.edit(RouteCount=RouteCount)

GeneratorIPv4Route = api.address_modifier(Start=FirstRoute, Step=RouteCount, Offset=offset, Count=1000)

IPv4Route = api.generator_next(GeneratorIPv4Route)

IPv4Route = api.generator_next(GeneratorIPv4Route)

else:

BgpRoute = BgpIpv4RoutepoolConfig(upper=BgpSession)

BgpRoute.get()

BgpRoute.edit(FirstRoute=IPv4Route)

BgpRoute.edit(PrefixLength=mask)

BgpRoute.edit(RouteCount=RouteCount)

Start = IPv4Route

GeneratorIPv4Route = api.address_modifier(Start=Start, Step=RouteCount, Offset=offset, Count=1000)

IPv4Route = api.generator_next(GeneratorIPv4Route)

IPv4Route = api.generator_next(GeneratorIPv4Route)

# bgp参数修改

as_path_length = random.randint(2, AsPathMaxLength)

community_length = random.randint(2, CommunityMaxLength)

as_path_list = list()

community_list = list()

AsPathIncrement_list = list()

as_path_tem = str()

community_tem = str()

communityIncrement_list = list()

for z in range(as_path_length):

as_path = random.randint(300, 64000)

as_path_list.append(as_path)

for z in range(community_length):

community1 = random.randint(1, 65535)

community2 = random.randint(1, 65535)

community = str(community1) + ':' + str(community2)

community_list.append(community)

for i in range(len(community_list) - 1):

community = community_list[i]

community_tem += community

community_tem += ','

community_tem += str(community_list[-1])

AsPathPerBlockCount = int(RouteCount / 6.5)

for z in range(len(as_path_list)):

AsPathIncrement_list.append(1)

for z in range(len(community_list)):

Temp = str(1) + ':' + str(1)

communityIncrement_list.append(Temp)

BgpRoute.edit(AsPath=as_path_list)

BgpRoute.edit(AsPathIncrement=AsPathIncrement_list)

BgpRoute.edit(AsPathPerBlockCount=AsPathPerBlockCount)

BgpRoute.edit(Community=community_tem)

BgpRoute.edit(CommunityIncrement=communityIncrement_list)

BgpRoute.edit(CommunityPerBlockCount=AsPathPerBlockCount)

# IPv6路由block创建

FirstPrefix = start_ipv61

Ipv6PrefixCount = 0

for y in range(BgpRouteBlockv6):

mask = random.randint(PrefixMin, PrefixMax)

if y == BgpRouteBlock - 1:

RouteCount = ipv6PrefixPerSession - Ipv6PrefixCount

else:

RouteCount = random.randint(Ipv6CountRandonMin, Ipv6CountRandonMax)

Ipv6PrefixCount = Ipv6PrefixCount + RouteCount

offset = 128 - mask

if x == 0 and y == 0:

# bgp参数修改

BgpRoute = BgpIpv6RoutepoolConfig(upper=BgpSession)

BgpRoute.get()

BgpRoute.edit(FirstIpv6Route=FirstPrefix)

BgpRoute.edit(PrefixLength=mask)

BgpRoute.edit(RouteCount=RouteCount)

GeneratorIPv6Route = api.address_modifier(Start=FirstPrefix, Step=RouteCount, Offset=offset, Count=1000)

IPv6Prefix = api.generator_next(GeneratorIPv6Route)

IPv6Prefix = api.generator_next(GeneratorIPv6Route)

else:

BgpRoute = BgpIpv6RoutepoolConfig(upper=BgpSession)

BgpRoute.get()

BgpRoute.edit(FirstIpv6Route=IPv6Prefix)

BgpRoute.edit(PrefixLength=mask)

BgpRoute.edit(RouteCount=RouteCount)

Start = IPv6Prefix

GeneratorIPv6Route = api.address_modifier(Start=Start, Step=RouteCount, Offset=offset, Count=1000)

IPv6Prefix = api.generator_next(GeneratorIPv6Route)

IPv6Prefix = api.generator_next(GeneratorIPv6Route)

# bgp参数修改

as_path_length = random.randint(2, AsPathMaxLength)

community_length = random.randint(2, CommunityMaxLength)

as_path_list = list()

community_list = list()

AsPathIncrement_list = list()

as_path_tem = str()

community_tem = str()

communityIncrement_list = list()

for z in range(as_path_length):

as_path = random.randint(300, 64000)

as_path_list.append(as_path)

for z in range(community_length):

community1 = random.randint(1, 65535)

community2 = random.randint(1, 65535)

community = str(community1) + ':' + str(community2)

community_list.append(community)

for i in range(len(community_list) - 1):

community = community_list[i]

community_tem += community

community_tem += ','

community_tem += str(community_list[-1])

AsPathPerBlockCount = int(RouteCount / 6.5)

for z in range(len(as_path_list)):

AsPathIncrement_list.append(1)

for z in range(len(community_list)):

Temp = str(1) + ':' + str(1)

communityIncrement_list.append(Temp)

BgpRoute.edit(AsPath=as_path_list)

BgpRoute.edit(AsPathIncrement=AsPathIncrement_list)

BgpRoute.edit(AsPathPerBlockCount=AsPathPerBlockCount)

BgpRoute.edit(Community=community_tem)

BgpRoute.edit(CommunityIncrement=communityIncrement_list)

BgpRoute.edit(CommunityPerBlockCount=AsPathPerBlockCount)

# Save the test case to D:\ as the name of bgp_random.xcfg

save_case = SaveTestCaseCommand(TestCase='D/:bgp_random.xcfg', ProductType=1)

save_case.execute()

随机路由生成测试

执行代码后,生成配置如下:

  • 接口参数:

  • BGP Session

  • IPv4路由

随机变化点:路由数量、路由前缀长度、AsPath、AsPath跳变步长、AsPath数量、Community、Community跳变步长、Community数量

  • IPv6路由

随机变化点:路由数量、路由前缀长度、AsPath、AsPath跳变步长、AsPath数量、Community、Community跳变步长、Community数量

DarYu-X系列测试仪

DarYu-X系列高性能网络测试仪是信而泰推出的面向高端路由器等高端数通设备的测试产品,具有高性能、高密度、高速率等特点,配置信而泰基于PCT架构的新一代测试软件RENIX和X系列测试模块,可为提供路由哭喊组网测试解决方案,为建立一张高SLA保证、确定性时延、业务感知、灵活业务路径调优的下一代网络保驾护航。

标签:edit,list,community,api,随机,BgpRoute,自动化,路由
From: https://www.cnblogs.com/xinertel/p/17563396.html

相关文章

  • python计算随机口算测试
    Python计算随机口算测试在学习数学的过程中,口算是培养孩子计算能力的重要方法之一。通过口算练习,孩子们可以提高他们的计算速度和准确性。然而,每天给孩子们出一百道口算题并不是一件容易的事情,这就是为什么我们需要计算机的帮助。在本文中,我们将介绍如何使用Python编写一个随机口......
  • VLAN间路由
    VLAN间路由每个路由器接口对应一个VLAN例6-1配置每个路由器接口对应一个VLANR1#configureterminalR1(config)#interfacef0/0R1(config-if)#ipaddress172.17.10.1255.255.255.0R1(config-if)#noshutdownR1(config-if)#interfacef0/1R1(config-if)#ipaddre......
  • 第2章 静态路由
    第2章静态路由等价负载均衡:当路由器有多条到达目的网络的路径,并且这些路径的度量值(跳数、带宽等)(即所谓的等开销度量),路由器将进行等价负载均衡。启用debugiprouting命令调试R2#debugiprouting关闭debugR2#undebugall添加直连路由:R2(config)#intfa0/0R2(config-i......
  • 审批自动化发布
    发布申请流程发布申请LarkPC或APP端通过「工作台」-「审批」查找创建飞书审批飞书&LarkPC或APP端:「工作台」-「审批中心」-「管理后台」创建审批审批设计保存approval_codeid订阅审批事件时需要创建应用开发者后台-创建应用https://open.larksuite.com/app添加订阅事件提......
  • R语言逻辑回归(Logistic Regression)、回归决策树、随机森林信用卡违约分析信贷数据集
    原文链接:http://tecdat.cn/?p=23344最近我们被客户要求撰写关于信用卡违约分析的研究报告,包括一些图形和统计输出。本文中我们介绍了决策树和随机森林的概念,并在R语言中用逻辑回归、回归决策树、随机森林进行信用卡违约数据分析决策树是由节点和分支组成的简单树状结构。根据每......
  • 数据分享|用加性多元线性回归、随机森林、弹性网络模型预测鲍鱼年龄和可视化|附代码数
    原文链接:http://tecdat.cn/?p=24127最近我们被客户要求撰写关于预测鲍鱼年龄的研究报告,包括一些图形和统计输出。鲍鱼是一种贝类,在世界许多地方都被视为美味佳肴养殖者通常会切开贝壳并通过显微镜计算环数来估计鲍鱼的年龄。因此,判断鲍鱼的年龄很困难,主要是因为它们的大小不仅......
  • pytest---更改自动化测试用例执行顺序
    前言在自动化测试中,自动化测试用例设计原则就是执行过程时不能存在依赖顺序,那么如果测试用例需要按照指定顺序执行,这个时候应该怎么做呢?目前单元测试框架中unittest没有办法改变测试用例的执行顺序,但是另一个单元测试框架pytest可以做到,辅助测试人员更改测试用例的执行顺序,今天小......
  • adb如何做Android ui自动化(这一篇就够了)
    一.简介我们都知道在做Androidui自动化的时候用的是appium,环境搭建贼难受。如果我们在工作中遇到需要实现简单的自动化功能,可以直接使用adb来完成,无需去搭建繁琐的appium。ADB(AndroidDebugBridge)是一个用于在Android设备和计算机之间传输数据、安装应用程序、调试和测试Androi......
  • 直播app开发搭建,JS生成随机数,生成指定位数的随机数
    直播app开发搭建,JS生成随机数,生成指定位数的随机数//获取随机数,小数第一位可能为0console.log(Math.random()); //获取10位随机数,如果小数第一位为0则只有9位数console.log(Math.floor(Math.random()*Math.pow(10,10))); //随机数+1,解决小数第一位为0的情况//但是会导致随机......
  • 【6.0】Django框架之路由层
    【一】路由匹配#路由匹配path('test',views.test),path('testadd',views.testadd),无法跳转到testaddurl方法第一个参数是正则表达式只要第一个参数正则表达式能够匹配到内容,就会立刻停止匹配,执行视图函数#路由匹配path('test/',views.test),path('testadd......