心血来潮,突然想到做一个根据MAC号查询对应厂商信息的程序。方法无外几种:
1、存储所有OUI信息表,根据MAC查询。需要存储介质进行存储。
2、使用网络服务查询。需要联网。
ieee上有个OUT文本文件:http://standards-oui.ieee.org/oui/oui.txt,里面包括了很多OUT信息,但不确定是不是最新的。
很多网站支持在线查询MAC对应厂商信息。比如wireshark:https://www.wireshark.org/tools/oui-lookup.html,支持MAC、厂商名称字符串查询。而这个:http://www.macvendorlookup.com/mac-address-api地址则支持生成JSON、XML格式的数据。
下面使用python解析JSON格式的数据。源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #!/usr/bin/python3 # encoding: utf-8 # 根据mac号查询对应的公司名字 # TODO 解析一个conf文件,而不是写死mac # 注意:如果使用urllib库,源码文件不能是urllib.py。。。。 import urllib.request import json mac_addr = ["a4:44:d1", "08:00:20", "fc:d7:33", "da:a1:19"] def simple_test(): for i in range(len(mac_addr)): oui_url = ("http://www.macvendorlookup.com/api/v2/" + mac_addr[i]) request = urllib.request.Request(oui_url) response = urllib.request.urlopen(request) #print(response.read()) # 打印网页源码 #print(response.read()) encodedjson = bytes.decode(response.read()) # bytes to string #print(encodedjson) if (encodedjson == ''): #print("empty....") continue decodejson = json.loads(encodedjson) #d1 = json.dumps(decodejson,sort_keys=True,indent=4) #print(d1) #print(type(decodejson)) # 这时已经是一个list # 打印需要的字段 print("mac地址:" + mac_addr[i] + "\t" + decodejson[0]["country"] + " 公司:" + decodejson[0]["company"] + "\t地址:" + decodejson[0]["addressL3"]) #### main if __name__ == '__main__': print("url test") simple_test()
|
注:好像源码给出的网站访问不太正常,我经常连接失败。
李迟 2016.9.11 周日 晚,桂林母校归来