huawei 用expect来获取AC里面WLAN的MAC 地址名单,运行脚本,信息会输出到屏幕
1get.switch.wlan.grep.mac.sh 用正则表达式过滤MAC地址,保存纯MAC到文件中
2.exception.py 用来把从AC获取的MAC与GLPI的MAC 进行比较,把不在GLPI里面的MAC输出到文件
安装Python-mysql库 [root@pc0003 1123diff.from.ac.glpi]# yum install -y MySQL-python
[root@pc0003 1123diff.from.ac.glpi]# cat huawei #!/usr/bin/expect -f set ip 192.168.AC.IP set password ******** set timeout 1 spawn ssh admin@$ip expect { "*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r" } } expect "<ac001>" send "sys\r" expect "\[ac001\]" send "wlan\r" expect "wlan-view" send "sta-whitelist-profile id 1\r" expect "prof-vobile" send "d th\r" expect "More" send "\t" expect "More" send "\t" expect "More" send "\t" expect "More" send "\t" expect "More" send "\t" expect eof #interact
[root@pc0003 1123diff.from.ac.glpi]# cat 1get.switch.wlan.grep.mac.sh #!/bin/bash # 此脚本正则匹配MAC,输出原格式的纯MAC地址 #指定分割符 split="-" mkdir tmp > /dev/null 2>&1 ./huawei |grep "sta-mac" > ./tmp/huawei.mac.txt stat=`echo $?` if [ $stat==0 ] then echo "已获取到交换机mac白名单" else echo "与交换机联络失败" fi #1a?2b?3d?4g?5k?6h #cat mac.txt | egrep -o "([0-9a-fA-F]{2})(([/\s$split][0-9a-fA-F]{2}){5})" > mac.txt #1qaz?2wsx?3edc cat ./tmp/huawei.mac.txt | egrep -o "([0-9a-fA-F]{4})(([/\s$split][0-9a-fA-F]{4}){2})" > ./tmp/grep.switch.mac.txt stat=`echo $?` if [ $stat==0 ] then echo "纯MAC地址文件已经输出: ./tmp/grep.switch.mac.txt" echo "格式如下" head ./tmp/grep.switch.mac.txt fi
[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
本文出自 “折腾岁月。” 博客,谢绝转载!
expect shell python glpi exception.mac
原文:http://990487026.blog.51cto.com/10133282/1716386