前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python MAC地址 获取,过滤,转

Python MAC地址 获取,过滤,转

作者头像
py3study
发布2020-01-10 01:11:03
2.3K0
发布2020-01-10 01:11:03
举报
文章被收录于专栏:python3python3

【输出MAC 地址】

代码语言:javascript
复制
[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-vob***"
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地址】

代码语言:javascript
复制
[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地址转换】

代码语言:javascript
复制
#!/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地址】

代码语言:javascript
复制
[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地址转换]

代码语言:javascript
复制
#!/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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档