多用户接入,过滤多余信息工具

Zss 发表于:

1.在测试多用户的时候需要抓包分析来判断 AP端到STA端的ping报文当发生重传的时候速率是否下降

2.所以将抓取的无线包在omnipeek中进行一次过滤,过滤原则为:ap的mac到任意的mac

3.将无线包导出为csv文件到脚本的同一目录下,命名为test.csv

4.脚本可以过滤掉多余的信息生成一个符合条件的txt,然后在txt中来来判断哪个终端出现了速率没有下降的问题

#coding:gbk
import csv
import os
import sys
reload(sys)
sys.setdefaultencoding('gbk')
class Many_users():
    def __init__(self):
        self.file_csv = []
        self.sta_list = []
        self.filter_info = []
        self.info = []
    def read_csv(self,file_path):
        '''
        csv文件读取
        '''
        try:
            with open(path,'r+') as file:
                file = csv.reader(file)
                for i in file:
                    self.file_csv.append(i)
        except:pass
        return self.file_csv

    def get_sta_mac(self,file_csv):
        test_number = len(file_csv)
        for i in range(2,test_number):
            if file_csv[i][2] not in self.sta_list:
                self.sta_list.append(file_csv[i][2])
        return self.sta_list

    def del_info(self,file_csv,filter_mac,sta_list):
        test_number = len(file_csv)
        for i in range(2,test_number):
            if file_csv[i][1] == filter_mac:
                if file_csv[i][5] == 'W+A':
                    self.filter_info.append(file_csv[i])
        for a in sta_list:
            for t in self.filter_info:
                if a == t[2]:
                    self.info.append(t)
        return self.info

    def results(self,info,file_path):
        try:
            for i in range(0,len(info)):
                with open(file_path,'a') as n:
                    if int(info[i+1][0])-int(info[i][0]) <= 2:
                        if int(info[i+2][0])-int(info[i+1][0]) <= 2:
                            if int(info[i+3][0])-int(info[i+2][0]) <= 2:
                                o = '数据包位置:{},     源mac:{},     目的mac:{},     flag:{},     速率:{}'.\
                                    format(str(info[i][0]),str(info[i][1]),str(info[i][2]),str(info[i][5]),str(info[i][8]))
                                n.write(o)
                                n.write('\n')
        except:
            pass
if __name__=='__main__':
    print('-------------------------------------------------------------------------------')
    print(' 仅限于多用户分析过滤多余信息,请关闭csv文件运行                                     ')
    print(' 使用方法:omnipeek 使用源mac到任意mac过滤一次,然后导出为test.csv文件放入脚本同一目录')
    print(' 过滤依据:1.过滤掉flag不为W+A的重传标志')
    print('           2.过滤掉非用户输入的源MAC')
    print('           3.过滤掉连续三个重传包之间编号差值都大于2')
    print(' 输出格式:编号 + 源Mac + 目的Mac + Flag + 速率                        ')
    print(' 输出位置:输出文本为脚本目录下的test.txt,若存在此txt则覆盖,若不存在则创建                        ')
    print('--------------------------------------------------------------------------------')
    print('           请输入需要过滤的源MAC地址,例如:54:EE:75:98:94:C6,不区分大小写')
    filter_mac = raw_input()
    while 1:
        raw_input('-----------------------------  输入回车开始过滤  -----------------------------')
        path = sys.argv[0]
        path = os.path.split(path)[0]+'/test.csv'
        file_path = os.path.split(path)[0]+'/test.txt'
        if os.path.exists(file_path):
            os.remove(file_path)
        test_csv = Many_users()
        flie_csv = test_csv.read_csv(file_path)
        sta_list = test_csv.get_sta_mac(flie_csv)
        print('                                 请稍后...       ')
        filter_mac = filter_mac.upper()
        info = test_csv.del_info(flie_csv,filter_mac,sta_list)
        test_csv.results(info,file_path)
        print('                   过滤完成,文件已生成                          ')
        print('                   替换掉csv文件,回车再次过滤新的csv文件')