Python+Json实现Xena L23网络测试仪脚本控制,能够灵活适用用多种测试案例

硬件:XENA Valkyrie 或 Vantage主机,测试板卡不限,本方法适用于其100M~400G所有速率端口

环境配置:Python 3

实现功能:

1.控制流量仪进行流量测试,预定配置的流量发送,报文统计,丢包率,延迟等信息的统计

2.单个Python脚本配合不同的JSON对象文件,来实现不同端口数量,不同拓扑模型下的流量测试

3.在发包测试前及运行过程中都对发包端口的链路状态进行监控100M/1G/2.5G/5G/10G

4.嵌入ARP探测,获取DUT的MAC信息,并记录在测试日志中

5.统计端口流量的Tx,Rx信息,丢包率,延迟信息

 Json配置文件

{
"##常用配置修改":"",

"ports": ["0/0","0/1","0/2","0/3","0/4","0/5"],
"packet": "random 64 1518",
"testtime": "30",

"##注意:dvlan和lanrate只有在Aggregation模式生效,其他模式仅需uvlan和wanrate":"",
"##其中vlan设置为-1则表示不添加vlan":"",
"uvlan": "-1",
"dvlan": "-1",
"wanrate": ["100", "100", "100","100", "100", "100"],
"lanrate": ["21", "22", "23","24","21", "22", "23","24"],

"portrate": ["1000","1000","1000","1000","1000","1000"],

"##Stype为测试模式,分别可以配置为:Loopback, Eachother, Aggregation":"",
"##其中Loopback为环回测试,Eachother为两两互打测试,Aggregation为汇聚测试":"",
"Stype":"Eachother",

"##headertype为报文类型,分别可以配置为:TCP, UDP, IP, Ethernet":"",
"headertype": "IP",

"##threshold为丢包率设置":"",
"threshold": "0",




"##不常用配置修改":"",

"##发送学习报文的时间":"",
"learntime": "3",

"##payload类型分别有: Pattern, Random":"",
"payload": "Pattern",

"##PRcheck为使能端口状态检测,1为开启,0为关闭;使能后端口为连接的时候会报错":"",
"PRcheck": "1",

"##PScheck为检测端口速率是否匹配,1为开启,0为关闭":"",
"PScheck":"1",

"tcpUdpPort": ["1024", "2048"],
"IP": "192.168.163.1",
"mac": "000000033333",

"##学习DUT的MAC地址":"",
"snlernenable": "0",
"learnIP": "192.168.2.1",

"##测试仪IP地址":"",
"ip_address": "192.168.1.200"
}

main.py

import threading, time, json, random, queue, types, sys, socket
from binascii import hexlify
from TestUtilsL23 import XenaScriptTools
from CreateClass import StreamCreate
from Statistics import CollectTestresult

def BuildDict(ports, configvalue, watching, watching_2):
	#Generate ports Dictionary
	a = 1 
	for port in ports:
		ip_address_b = configvalue['IP'][0:10]
		ip_address_c = ip_address_b + str(a)
		mac_a = configvalue['mac'][0:10]
		if a > 9:
			mac_address = mac_a + str(a)
		else:
			mac_address = mac_a + '0' + str(a)
		ip_address_a = hexlify(socket.inet_aton(ip_address_c)).decode()
		mac_ip = [str(mac_address), str(ip_address_a)]
		watching[port] = mac_ip
		a += 1

	watching_2['type_eth'] = 'FFFF',
	watching_2['type_vlan'] = '8100',
	watching_2['type_ip'] = '08004500006A000000007F11BA2D',
	watching_2['type_udp'] = '00000000',
	watching_2['type_tcp'] = '000000000000000050000000E5A40000',
	watching_2['tcpid'] = [hex(int(configvalue['tcpPort'][0]))[2:].zfill(4) + hex(int(configvalue['tcpPort'][1]))[2:].zfill(4)]


def PrintMes(ports, configvalue, ip_address):
	localtime = time.asctime( time.localtime(time.time()) )
	print("\n##############################################")
	print("                   测试配置                   ")
	print("##############################################\n")
	print("测试时间     :  " + str(localtime))
	print("设备IP地址   :  " + ip_address)
	print("测试端口     :  " + str(ports))
	print("测试模式     :  " + configvalue['Stype'])
	print("持续时间     :  " + configvalue['testtime'])
	print("报文类型     :  " + configvalue['header_type'])
	print("报文长度     :  " + configvalue['packet'])
	if configvalue['Stype'] == 'Aggregation':
		print("上行VLAN     :  " + str(configvalue['uvlan']))
		print("下行VLAN     :  " + str(configvalue['uvlan']))
		print("WAN速率      :  " + str(configvalue['wanrate']))
		print("LAN速率      :  " + str(configvalue['lanrate']))
	else:
		print("VLAN         :  " + str(configvalue['uvlan']))
		print("速率设置     :  " + str(configvalue['wanrate']))
	print("端口速率     :  " + str(configvalue['portrate']))
	print("学习时间     :  " + configvalue['learntime'])
	print("丢包率设置   :  " + configvalue['threshold'])
	if configvalue['snlearnenable'] == '1':
		print("自动学习SN   :  Enable")
	else:
		print("自动学习SN   :  Disable")
	if configvalue['portspeedcheck'] == '1':
		print("端口速率检测 :  Enable" )
	else:
		print("端口速率检测 :  Disable" )


def Maclearning(xm, ports, configvalue):
	if configvalue['snlearnenable'] == '1':
		xm.SendExpectOK(ports [1] + " PS_CREATE [0]")
		xm.SendExpectOK(ports [1] + " ps_tpldid [0] 0")
		xm.SendExpectOK(ports [1] + " PS_PACKETLENGTH [0] FIXED 70 1518")
		xm.SendExpectOK(ports [1] + " PS_HEADERPROTOCOL [0] ETHERNET ARP")
		learnIP_hex = hexlify(socket.inet_aton(configvalue['learnIP'])).decode()
		xm.SendExpectOK(ports [1] + " PS_PACKETHEADER [0] 0xFFFFFFFFFFFF00000000000108060001080006040001000000000001C0A80164FFFFFFFFFFFF" + learnIP_hex)
		xm.SendExpectOK(ports [1] + " PS_RATEPPS [0] 2")
		xm.SendExpectOK(ports [1] + " PS_ENABLE [0] on")
		xm.SendExpectOK(ports [1] + " P_TRAFFIC ON")
		xm.SendExpectOK(ports [1] + " P_CAPTURE ON")
		time.sleep(1)
		xm.SendExpectOK(ports [1] + " P_CAPTURE OFF")
		xm.SendExpectOK(ports [1] + " P_TRAFFIC OFF")
		SN = xm.Send(ports[1] + " PC_PACKET [0] ?")
		SN = SN.split('  ')[-1]
		xm.SendExpectOK(ports [1] + " PS_DELETE [0]")
		Serial = str(SN)
		if SN == '<BADINDEX>':
			print ("\nTest result :Failed;[ARP learning failed!!]")
			sys.exit()
		print ("DUT SN       :  " + str(Serial[16:28]))


def PortSpeed(xm, ports, PSC, configvalue):
	if configvalue['PRcheck'] == '1':
		xm.PortSyncCheck(ports)
	i = 0
	for port in ports:
		PS = xm.Send(port + ' p_speed ?')
		PS = PS.split('  ')[-1]
		PSC.append(str(PS))
		if configvalue['portspeedcheck'] == '1':
			if str(PS) != configvalue['portrate'][i]:
				print ('Test Result:Failed;[' + port + ' 端口速率不匹配,请检查配置...]')
				sys.exit()
		i += 1


def runtest(xm, ports, configvalue):
	xm.PortTrafficStart(ports)
	time.sleep(float(configvalue['learntime']))
	xm.PortTrafficStop(ports)
	time.sleep(0.5)
	xm.Statisticsclear(ports)
	time.sleep(0.5)
	xm.Porttimelimit(ports, configvalue['testtime'])
	time.sleep(0.2)
	xm.PortTrafficStart(ports)
	count = 0
	print ("\n开始打流,请耐心等候, 测试时间约为" + configvalue['testtime'] + "秒......\n")
	while True:
		print (">>>>>>>>>", end='')
		xm.Send(ports[0] + " P_TRAFFIC ?")
		sys.stdout.flush()
		count += 1
		time.sleep(1)
		if count > int(configvalue['testtime']):
			print ('\n\n测试结束,正在收集测试数据......')
			break
	print()
	xm.PortTrafficStop(ports)
	time.sleep(1)


def main(argv):
	a = sys.argv
	configvalue = {}
	filename = str(a[1])
	with open(filename,'rb') as f:
		configs=json.loads(f.read())

		ip_address = configs.get('ip_address')
		ports = configs.get('ports', '')
		configvalue['uvlan'] = configs.get('uvlan', '')
		configvalue['dvlan'] = configs.get('dvlan', '')
		configvalue['tcpPort'] = configs.get('tcpUdpPort', '')
		configvalue['packet'] = configs.get('packet', '')
		configvalue['testtime'] = configs.get('testtime', '')
		configvalue['threshold'] = configs.get('threshold', '')
		configvalue['header_type'] = configs.get('headertype', '')
		configvalue['learntime'] = configs.get('learntime', '')
		configvalue['payload'] = configs.get('payload', '')
		configvalue['wanrate'] = configs.get('wanrate', '')
		configvalue['lanrate'] = configs.get('lanrate', '')
		configvalue['portrate'] = configs.get('portrate', '')
		configvalue['PRcheck'] = configs.get('PRcheck', '')
		configvalue['IP'] = configs.get('IP', '')
		configvalue['mac'] = configs.get('mac', '')
		configvalue['snlearnenable'] = configs.get('snlernenable', '')
		configvalue['learnIP'] = configs.get('learnIP', '')
		configvalue['Stype'] = configs.get('Stype', '')
		configvalue['portspeedcheck'] = configs.get('PScheck', '')

	PSC = []
	FCS = []
	watching = {}
	watching_2 = {}
	testresult = {}

	PrintMes(ports, configvalue, ip_address)

	xm = XenaScriptTools(ip_address)
	xm.LogonSetOwner("xena", "python_test_1")
	xm.PortRelinquish(ports)
	xm.PortReserve(ports)
	xm.PortTrafficStop(ports)
	xm.StreamsDelete(ports)
	sc = StreamCreate(xm, ports, watching, watching_2, configvalue)
	cr = CollectTestresult(xm, ports, PSC, configvalue, testresult, FCS)

	
	BuildDict(ports, configvalue, watching, watching_2)
	PortSpeed(xm, ports, PSC, configvalue)
	if configvalue['snlearnenable'] == '1':
		Maclearning(xm, ports, configvalue)
	if configvalue['Stype'] == 'Loopback':
		sc.Loopback(xm, ports, watching, watching_2, configvalue)
		runtest(xm, ports, configvalue)
		cr.LoopbackStatistics(xm, ports, testresult)
	if configvalue['Stype'] == 'Eachother':
		sc.Eachother(xm, ports, watching, watching_2, configvalue)
		runtest(xm, ports, configvalue)
		cr.EachotherStatistics(xm, ports, testresult)
	if configvalue['Stype'] == 'Aggregation':
		sc.Aggregation(xm, ports, watching, watching_2, configvalue)
		runtest(xm, ports, configvalue)
		cr.AggregationStatistics(xm, ports, testresult)
	cr.FCSGetValue(xm, ports, FCS)
	cr.OutputStatistics(ports, PSC, configvalue, testresult, FCS)
	xm.PortRelease(ports)

if __name__ == '__main__':
    sys.exit(main(sys.argv))
发布了9 篇原创文章 · 获赞 0 · 访问量 742

猜你喜欢

转载自blog.csdn.net/pangzi_sh/article/details/103285113