【输出MAC 地址】
[root@pc0003 glpi_switch_ocs]# cat huawei
#!/usr/bin/expect -f
set ip 192.168.AC.IP
set password AC.PASSWORD
set timeout 1
spawn ssh AC.USER@$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 glpi_switch_ocs]#
【过滤MAC地址】
[root@pc0003 glpi_switch_ocs]# 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
【MAC地址转换】
#!/usr/bin/python new1=open(‘./tmp/convert.switch‘,‘w‘) import re import os r1=r"\w\w\w\w-\w\w\w\w-\w\w\w\w" for i in open(‘./tmp/grep.switch.mac.txt‘): str_upper=i.upper() string1=str(re.findall(r1,str_upper)) list1=list(string1) while ‘[‘ in list1: list1.remove(‘[‘) while ‘\‘‘ in list1: list1.remove(‘\‘‘) while ‘-‘ in list1: list1.remove(‘-‘) while ‘]‘ in list1: list1.remove(‘]‘) new1.write(list1[0]+list1[1]+":"+list1[2]+list1[3]+":"+list1[4]+list1[5]+":"+list1[6]+list1[7]+":"+list1[8]+list1[9]+":"+list1[10]+list1[11]+"\n") new1.close()
【获取GLPI的MAC地址】
[root@pc0003 glpi_switch_ocs]# cat 3get.glpi.sh
#!/bin/bash
# 此脚本正则匹配MAC,输出原格式的纯MAC地址
mkdir tmp > /dev/null 2>&1
#指定分割符
split=":"
mysql -h 192.168.GLPI.MYSQL.IP -pGLPI.MYSQL.PASSWORD -Ne "use glpi;select glpi_items_devicenetworkcards.mac from glpi_items_devicenetworkcards,glpi_devicenetworkcards where glpi_items_devicenetworkcards.devicenetworkcards_id = glpi_devicenetworkcards.id;" > tmp/glpi.all.mac
stat=`echo $?`
if [ $stat==0 ]
then
echo "已获取到glpi的mac所有清单"
else
echo "与mysql联络失败!"
fi
#1a?2b?3d?4g?5k?6h
cat tmp/glpi.all.mac | egrep -o "([0-9a-fA-F]{2})(([/\s$split][0-9a-fA-F]{2}){5})" > tmp/grep.glpi.mac.txt
stat=`echo $?`
if [ $stat==0 ]
then
echo "纯MAC地址文件已经输出:./tmp/grep.glpi.mac.txt"
echo "格式如下"
head ./tmp/grep.glpi.mac.txt
fi[MAC地址转换]
#!/usr/bin/python new1=open(‘./tmp/convert.glpi.mac‘,‘w‘) import re r1=r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w" for i in open(‘tmp/grep.glpi.mac.txt‘): str_upper=i.upper() string1=str(re.findall(r1,str_upper)) list1=list(string1) while ‘[‘ in list1: list1.remove(‘[‘) while ‘\‘‘ in list1: list1.remove(‘\‘‘) while ‘:‘ in list1: list1.remove(‘:‘) while ‘]‘ in list1: list1.remove(‘]‘) print list1 new1.write(list1[0]+list1[1]+":"+list1[2]+list1[3]+":"+list1[4]+list1[5]+":"+list1[6]+list1[7]+":"+list1[8]+list1[9]+":"+list1[10]+list1[11]+"\n") new1.close()
本文出自 “折腾岁月。” 博客,谢绝转载!
Python MAC地址 获取,过滤,转换 Python MYSQL 数据获取,比较
原文:http://990487026.blog.51cto.com/10133282/1716406