Huawei uses expect to get the MAC address list of the WLAN inside the AC, run the script, and the information will be output to the screen.
1get.switch.wlan.grep.mac.sh Filter MAC address with regular expression, save pure Mac to file
2.exception.py is used to compare Mac acquired from AC to Glpi mac, and output Mac not in Glpi to file
Install Python-mysql library [[email protected] 1123diff.from.ac.glpi]# Yum install-y Mysql-python
[[email protected] 1123diff.from.ac.glpi]# cat Huawei #!/usr/bin/expect -fset ip 192.168.ac. Ipset password ********set timeout 1spawn ssh [email protected] $ipexpect { "*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
[[email protected] 1123diff.from.ac.glpi]# cat 1get.switch.wlan.grep.mac.sh #!/bin/bash# This script matches the Mac, the output original format of the Pure MAC address # Specifies the delimiter split= "-" mkdir tmp > /dev/null 2>&1./huawei |grep "Sta-mac" > ./tmp/huawei.mac.txtstat = ' echo $? ' if [ $stat ==0 ]then echo "acquired to switch mac Whitelist" else echo "failed to contact the switch" 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?3edccat ./tmp/ huawei.mac.txt | egrep -o "([0-9a-fa-f]{4}) (([/\s$split][0-9a-fa-f]{4}) {2})" > ./tmp/grep.switch.mac.txtstat= ' echo $? ' if [ $stat ==0 ]thenecho ' Pure MAC address file has been output: ./tmp/grep.switch.mac.txt "echo " format as follows "Head ./tmp/grep.switch.mac.txtfi
[[email protected] 1123diff.from.ac.glpi]# cat 2.exception.py #!/usr/bin/python# Coding=utf-8 ' relies on host= "192.168.glpi.mysql.ip", user= "user1", passwd= "password", db= "database" Reliance on Huawei relies on 1get.switch.wlan.grep.mac.sh dependency: Huawei relies on: 1get.switch.wlan.grep.mac.sh function: Glpi mac that does not exist in the WLAN Find out the limitations: Use only with ac and MySQL database "#创建tmp文件夹import osimport retry : os.mkdir ( ' tmp ' ) except oserror, why: print "tmp folder has been created%s" % str (why) print "getting the Mac list of the current WLAN " 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 address, user, device model, approval source \ n ") import mysqldbprint" contacting Database "Conn=mysqldb.connect (host=" 192.168. Glpi.mysql.IP ", user=" user ", passwd=" passwd ", db=" Databases ") Cur=conn.cursor () Select=cur.execute (" use glpi; ") Print "Getting All Macs" 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&NBSP;==&NBSP;MAC): mark1=1 if ( mark1==0 ): macfile.write (str1+ ", unknow\n") Cur.close () Conn.commit ( ) Conn.close () macfile.close () print ' File save path ' +filedir finally get the file format as below:[[email protected] 1123diff.from.ac.glpi]# cat tmp/6.exception.mac-wlan.onboard.mac.error.list.csv Exception.macmac address, user, device model, approved source 20:7c:8f:76:*****,unknow34:de:1a:1b:*****,unknowdc:85:56:3d:*****,unknow00:16: Ea:bb:*****,unknow
This article is from "tossing the years." "blog, declined reprint!"
Expect Shell Python Glpi Exception.mac