[root@pc0003 1123diff.from.ac.glpi]# cat 2.exception.py
#!/usr/bin/python
#coding=utf-8
'''
依赖host="192.168.glpi.mysql.IP",user="user1",passwd="password",db="database"
依赖huawei
依赖1get.switch.wlan.grep.mac.sh
依赖:huawei
依赖:1get.switch.wlan.grep.mac.sh
功能:把WLAN中不存在GLPI的MAC 找出来
局限:仅用与AC 与MySql数据库
'''
#创建tmp文件夹
import os
import re
try :
os.mkdir( 'tmp' )
except OSError, why:
print "tmp 文件夹已经被创建%s" % str(why)
print "正在获取当前WLAN的MAC名单 "
os.system('./1get.switch.wlan.grep.mac.sh')
#文件保存路径
filedir='./tmp/6.exception.mac-wlan.onboard.mac.error.list.csv'
#创建csv文件
MacFile=open(filedir,'w')
MacFile.write("exception.mac\nMAC地址,用户,设备型号,批准来源\n")
import MySQLdb
print"正在联络数据库"
conn=MySQLdb.connect(host="192.168.glpi.mysql.IP",user="user",passwd="passwd",db="databases")
cur=conn.cursor()
select=cur.execute("use glpi;")
print"正在获取所有MAC"
select=cur.execute("select upper(glpi_items_devicenetworkcards.mac) from glpi_items_devicenetworkcards,glpi_devicenetworkcards where glpi_items_devicenetworkcards.devicenetworkcards_id = glpi_devicenetworkcards.id;")
info = cur.fetchmany(select)
for j in open('tmp/huawei.mac.txt'):
j=j.upper()
str1=str(re.findall('\w\w\w\w-\w\w\w\w-\w\w\w\w', j))
str1=str1.replace("-",":")
str1=str1.replace('[','')
str1=str1.replace(']','')
str1=str1.replace('\'','')
str1=str1[:2]+':'+str1[2:]
str1=str1[:8]+':'+str1[8:]
str1=str1[:14]+':'+str1[14:]
mark1=0
for i in info:
mac=str(i)[2:-3]
#MacFile.write(str(i)[2:-3]+",unknow\n")
if (str1 == mac):
mark1=1
if ( mark1==0 ):
MacFile.write(str1+",unknow\n")
cur.close()
conn.commit()
conn.close()
MacFile.close()
print '文件保存路径'+filedir
最终得到的文件格式为下面:
[root@pc0003 1123diff.from.ac.glpi]# cat tmp/6.exception.mac-wlan.onboard.mac.error.list.csv
exception.mac
MAC地址,用户,设备型号,批准来源
20:7C:8F:76:*****,unknow
34:DE:1A:1B:*****,unknow
DC:85:56:3D:*****,unknow
00:16:EA:BB:*****,unknow
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com