python 的 scapy库,实现网卡收发包的例子
问题:
测试时收发流采用TestCenter、SmartBit等仪表来进行。如果仍采用其进行自动化冒烟,则会带来效率低、成本高的问题。
解决方案:
采用网卡来收发流,虽然有性能统计上的缺陷,但可以验证一些基本功能,且经济。
采用scapy模块,
1-获取计算机网卡的iface,并预先设计好用哪些iface进行收发流;
2-conf.L2listen对各个iface进行监听
3-subprocess.Popen来调用tShark.exe启动抓包,也可以调用ping.exe构造ping包
4-sendp发送二层报文,send发送三层报文
5-sniff嗅探iface上的指定报文,可以有过滤条件
6-停止wireshark抓包
7-close关闭对iface的监听
讨论:
没有尝试采用sr1、srp来进行收发包。
整个过程相对比较清晰,而且步骤是成对出现,方便记忆。
sniff嗅探时,会丢掉iface前面出现的部分报文,这个问题可能是没有执行好监听和启动抓包导致。
没有对网卡的具体性能标准作出说明,可能需要摸着石头过河,如果发现网卡有不合适测试的,需要立即切换到仪表来测试。
#!usr/bin/envpython #-*-coding:utf-8-*- importos importsys importre importstruct importstring fromscapy.allimport* importsubprocess conf.use_pcap=True ''' cmd python fromscapy.allimport* ls(Ether()) ls(IP()) ls(ICMP()) send(IP(dst='1.2.3.4')/ICMP()) sendp(Raw("zhongxing"),iface='eth15',loop=1,inter=0.2,verbose=False) 设置inter参数来设置发送相邻两个包直接的时间间隔 设置timeout参数来设置等待应答的超时时间 设置retry参数来设置重试次数。 ''' printu"实现网卡发包" target=[] foriinrange(1,len(sys.argv)): m=sys.argv[i].split('=') ifm[0]=='-t': target.append(m[1]) ifm[0]=='-ip': target.append(m[1]) ifm[0]=='-mac': target.append(m[1]) print'test--',target print printu'获取网卡的iface' eth_local=[] a=repr(conf.route).split('\n') forxina: b=[] b=x.split('') foryinb: ifre.search('eth',y): eth_local.append(y) printu'去重复' c=[] c.append(eth_local[0]) foriinrange(0,len(eth_local),1): m=0 forjinrange(0,len(c),1): ifc[j]==eth_local[i]: m+=1 ifm==0: c.append(eth_local[i]) printc#['eth15','eth21','eth17'] printu'创建二层报文' src_mac='00:00:11:11:22:22' dst_mac='00:00:22:22:11:11' dst_ip='1.2.3.4' src_ip='5.6.7.8' src_port=1234 dst_port=5678 ##ls() ##ls(IP()) ##IP().show() ##lsc() pack_ip=IP(dst=dst_ip,src=src_ip,proto=1) ##ls(ICMP()) ##ls(UDP()) pack_icmp=ICMP(type=8) ##ls(Ether()) pack_ether=Ether(dst=dst_mac,src=src_mac,type=0x0800) info=Raw('zhongxing') t=str(pack_ether/pack_ip/pack_icmp/info) s=Ether(t) printu'待发送的报文为:',s.summary eth=c[1] printu'发送的网卡iface为%s\n'%eth printu'---------开始监听-发送icmp-嗅探icmp-关闭监听----------' printu'---------开始监听-------------' L2socket=conf.L2listen listen_socket=L2socket(type=ETH_P_ALL,iface=eth) printlisten_socket printconf.L2listen ####启动抓包 ##cmd='C:\ProgramFiles(x86)\Wireshark\tShark.exe' ##card_id=str(1) ##cap_file=str('H:\python\test.pcap') ##args=[cmd,"-i"+card_id,"-w",cap_file] ##print"*DEBUG*",args ##p=subprocess.Popen(args) printu'---------sendp()函数调用----------' sendp(s,iface=eth,verbose=False) ##printu'---------srp()函数调用----------' ##sr函数是Scapy的核心,这个函数返回两个列表, ##第一个列表是收到应答的包和其对应的应答, ##第二个列表是未收到应答的包, ##通常,我们需要调用别的函数来使得这两个返回值更易于阅读, ##help(srp) ##p=srp(s,iface=c[1],verbose=False) ##printp.show() printu'---------嗅探、过滤、保存pcap、读取pcap----------' ##printsniff.__doc__ ##pkts=sniff(iface='eth15',filter='icmp',count=3,prn=lambdax:x.summary()) ip='172.10.0.1' subprocess.Popen(["ping.exe",ip])#提供给sniff ##Ether/IP/ICMP172.10.1.124>172.10.0.1echo-request0/Raw ##Ether/IP/ICMP172.10.0.1>172.10.1.124echo-reply0/Raw ##Ether/IP/ICMP172.10.1.124>172.10.0.1echo-request0/Raw ##listen_socket1=L2socket(listen_socket) ##pkts=sniff(iface=eth,filter='icmp',count=20,timeout=10,L2socket=listen_socket) pkts=sniff(iface=eth,filter='icmp',count=20,timeout=10) try: if0以上这篇python的scapy库,实现网卡收发包的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。