如果mac地址与正则表达式不匹配,则删除一行

0x6upsns  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(248)

我编写了一些脚本,从radius服务器创建报告,但现在我想从该报告中删除一些行。我只查找mac地址与re.search(“^[ae26].*”,mac.lower()匹配的行
我用它做了一些测试以查看mac地址,但现在我想删除不匹配的内容。

with open('macs.txt') as f:
    macs = f.read().splitlines()

    for mac in macs:

            writer.writerow(csvdata)
        mac = mac.replace('.','')
        mac = mac.replace(':','')
        mac = mac.replace('-','')
        mac = ''.join(mac.split())
        mac = ":".join(["%s" % (mac[i:i+2]) for i in range(0, 12, 2)])
        #reformat macs
        match = re.search("^.[ae26].*", mac.lower())
        if match:
            print(mac)

这是我解析ise中csv的脚本之一,我想删除所有非随机MAC,因为我的目标是只找到随机MAC

def macaddress():

    for names in glob.glob('W:/NS/NS_Shared/ISE Reports/*RADIUS_Errors*.csv'):

        inpute_file = csv.DictReader(open(names))

        seen = set()
        for row in inpute_file:
                mac = row["'CALLING_STATION_ID'"]
                mac = mac.replace("'",'')
                DN = row["'NETWORK_DEVICE_NAME'"]
                DN = DN.replace("'",'')
                UN = row["'USER_NAME'"]
                UN = UN.replace("'",'')
                FR = row["'FAILURE_REASON'"]
                FR = FR.replace("'",'')
                LD = row["'LOGGED AT'"]
                LD = LD.replace("'",'')

                #print(mac,(MacLookup().lookup(mac)),DN,IP,Port)                      

                network_device_list(UN,mac,LD,DN,FR)

def network_device_list( UN,mac,LD,DN,FR):

        filename = 'H:/'+name +'.csv'
        write_header = not os.path.exists(filename)
        with open(filename, 'a', newline='') as csvfile:
            headers = ['User Name','MAC Address','OUI','Logged At','Failure Reason']
            writer = csv.DictWriter(csvfile,fieldnames=headers)
            if write_header:
                writer.writeheader()
            writer = csv.writer(csvfile)
            try:
                csvdata = (UN,mac,(MacLookup().lookup(mac)),LD,DN,FR)
            except KeyError:
                csvdata = (UN,mac,' No Data',LD,DN,FR)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题