侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

遍历交换机里面的mac地址,与公司登记的合法mac地址进行对比

2022-07-09 星期六 / 0 评论 / 0 点赞 / 55 阅读 / 9930 字

脚本目的:查找mac黑名单日 期:2015年08月20日联系邮箱:[email protected] Q 群:1851 1570151CTO博客首页:http://990487026.blog.51

.



脚本目的:查找mac黑名单

日    期:2015年08月20日
联系邮箱:[email protected]
Q Q  群:1851 15701
51CTO博客首页:http://990487026.blog.51cto.com

开源社区,有你更精彩!



简介:

遍历交换机里面的mac地址,与公司登记mac合法的mac地址进行对比,匹配到了是合法的,未匹配到就是黑名单。



需求分析:查找mac黑名单


公司登记的PC,MAC,Server设备的mac地址,有一个表单 rmac文件

公司登记的是这样的格式 94-de-80-61-**-**


实际在交换机里面会与公司登记的mac1情况不一样,那么把交换机的mac地址,telnet界面复制出来,smac文件保存起来

实际交换机复制出来是这样的格式

IP Address       MAC Address     VLAN ID  Port Name / AL ID      Aging     Type
192.168.**.**     fa16-3e2f-a4**  200      GigabitEthernet1/0/**  11        D


那么问题来了,我要将公司登记的mac地址与交换机里面存在的mac地址,逐一比对,匹配到了,就OK,如果循环结束,没有被匹配到的,那么我是否可以理解为,设个设备是没有经过公司的登记,私自入网的,或者相关登记人员,在登记时,因一时疏忽,写错了其中mac地址的几位,比如,把8写成B,把E写成F,等。


问题又来了,我们需要允许出错的误差范围,mac地址由12位组成,一般情况下,允许错一位,或者2位吧。


=================================================================

执行脚本的条件:

-rwxr-xr-x. 1 root root 4.3K 7月   3 16:35 rmac

-rwxr-xr-x. 1 root root  34K 7月   3 16:35 smac


公司登记的mac地址文本rmac文件,格式如:94-de-80-61-**-**

公司交换机的mac地址文本smac文件 格式如下 :

192.168.**.**     fa16-3e2f-a4**  200      GigabitEthernet1/0/**  11        D



执行脚本,会提示输入位数的误差范围,

比如:输入 0 ,就是精准匹配,一边显示匹配结果,一边保存匹配到的文件mac_range_list

没有被匹配到的,就会生成黑名单  black_list


-rwxr-xr-x. 1 root root  15K 7月   3 16:35 black_list

-rwxr-xr-x. 1 root root 6.3K 7月   3 16:35 mac_range_list


因为我安装了python2.7,所以首行这么写。


说明:该脚本跳过对比 192.168.6.1.0网段,与192.168.200.0网段


================脚本开始==========================


#!/usr/local/python27/bin/python2.7#screen clsprint "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n"print "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n"print "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n"print "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n"new1=open('mac_range_list','w')new2=open('black_list','w')import rekong=[]keystr=raw_input("Enter Error Range [0-12]  ")key=int(keystr)new1.write("Error Range "+keystr+"/n")r1=r"/w/w/w/w-/w/w/w/w-/w/w/w/w"r2=r"/w/w-/w/w-/w/w-/w/w-/w/w-/w/w"stat2=1stat4=0stat5=0# open switch mac for i in open('smac'):    # judge i is empty ?      judge1=i.find("192")    if (judge1!=0):        continue    # as space split a list    b1=i.split(' ')             # remove empty element     while '' in b1:                     b1.remove('')    #b[0] is ip    sip=b1[0]    # as "." split ip ziduan    sip_list=sip.split(".")    #sip_list[2] is wang duan    sip_3=sip_list[2]    sip_3=int(sip_3)    # skip 1 and 200 wangduan     if (sip_3==1):         continue    if (sip_3==200):         continue    #print sip_3       #b1[1] is mac    c1= b1[1]    d1=c1.upper()    e1=d1.replace("-","")    #print e1    smac_list=list(e1)        q=smac_list    #print smac_list       len1=len(smac_list)    #print len1    stat3=0        for j in open('rmac'):        b2=j.upper()        r2_list=re.findall(r2,b2)        # remove kong         if r2_list==kong:            continue        l2=b2.replace("-","")        c2=l2.replace('/"',"")        #print c2        regmac_list=list(c2)        while '/n' in regmac_list:            regmac_list.remove('/n')            while '/"' in regmac_list:            regmac_list.remove('/"')        w=regmac_list         #stat5+=1        #print regmac_list,stat5        #count error range ip & mac        stat1=0        #range conut        for k in range(0,len1):            if smac_list[k]==regmac_list[k]:                stat1+=1         # black count  smac recoder        if (smac_list==regmac_list):            stat3=1            #print e1,c2        if (stat1==len1-key):             stat2str=str(stat2)            stat2str=str(stat2)            new1.write(stat2str+":switch   mac:"+q[0]+q[1]+"-"+q[2]+q[3]+"-"+q[4]+q[5]+"-"+q[6]+q[7]+"-"+q[8]+q[9]+"-"+q[10]+q[11]+"       IP:"+sip+"/n")            new1.write(stat2str+":regsiter mac:"+w[0]+w[1]+"-"+w[2]+w[3]+"-"+w[4]+w[5]+"-"+w[6]+w[7]+"-"+w[8]+w[9]+"-"+w[10]+w[11]+"/n/n")            new1.write("/n")            print "%s :switch   Mac  :%s%s-%s%s-%s%s-%s%s-%s%s-%s%s     IP:%s"  %(stat2,q[0],q[1],q[2],q[3],q[4],q[5],q[6],q[7],q[8],q[9],q[10],q[11],sip)             print "%s :register Mac  :%s%s-%s%s-%s%s-%s%s-%s%s-%s%s         /n "%(stat2,w[0],w[1],w[2],w[3],w[4],w[5],w[6],w[7],w[8],w[9],w[10],w[11])             stat2+=1        if(stat3==0):        stat4+=1        str4=str(stat4)        new2.write(str4+"  :"+i)    #break    new1.close()print "Error Range %s" %keyprint "total %s: " %(stat2-1)

================脚本结束=============================================



谢谢大家浏览,如对本文存在疑问,请至邮箱[email protected],开源社区,有你更精彩!


.

广告 广告

评论区