夜勿眠 发表于 2018-8-28 12:47:28

expect shell python glpi exception.mac

# 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_devicenetworkcardswhere 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
  
    str1=str1[:8]+':'+str1
  
    str1=str1[:14]+':'+str1
  
    mark1=0
  

  
    for i in info:
  
      mac=str(i)
  
      #MacFile.write(str(i)+",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
  

  

  
最终得到的文件格式为下面:
  
# 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]
查看完整版本: expect shell python glpi exception.mac