-
Notifications
You must be signed in to change notification settings - Fork 14
/
macvendor.py
183 lines (138 loc) · 4.84 KB
/
macvendor.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# This file parses the MAC file and allow us to efficiently retrieve the vendor of a MAC
# Part of this file was copied from https://github.com/coolbho3k/manuf
from collections import namedtuple
import re
import os
import io
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
# Vendor tuple
Vendor = namedtuple('Vendor', ['manuf', 'manuf_long', 'comment'])
_masks = {}
def refresh():
global _masks
"""Refresh/reload manuf database. Call this when manuf file is updated.
Args:
fname (str): Location of the manuf data base file. Defaults to "manuf" in the
same directory.
Raises:
IOError: If manuf file could not be found.
"""
fname='oui.txt'
with io.open(fname, "r", encoding="utf-8") as read_file:
ffile = StringIO(read_file.read())
# Build mask -> result dict
for line in ffile:
try:
line = line.strip()
if not line or line[0] == "#":
continue
line = line.replace("\t\t", "\t")
fields = [field.strip() for field in line.split("\t")]
parts = fields[0].split("/")
mac_str = _strip_mac(parts[0])
mac_int = _get_mac_int(mac_str)
mask = _bits_left(mac_str)
# Specification includes mask
if len(parts) > 1:
mask_spec = 48 - int(parts[1])
if mask_spec > mask:
mask = mask_spec
comment = fields[3].strip("#").strip() if len(fields) > 3 else None
long_name = fields[2] if len(fields) > 2 else None
_masks[(mask, mac_int >> mask)] = Vendor(manuf=fields[1], manuf_long=long_name, comment=comment)
except:
print("Couldn't parse line", line)
raise
ffile.close()
def _get_mac_int(mac_str):
# Gets the integer representation of a stripped mac string
try:
# Fill in missing bits with zeroes
return int(mac_str, 16) << _bits_left(mac_str)
except ValueError:
raise ValueError("Could not parse MAC: {0}".format(mac_str))
def _strip_mac(mac):
# Strips the MAC address of '-', ':', and '.' characters
# Regular expression that matches '-', ':', and '.' characters
_pattern = re.compile(r"[-:\.]")
return _pattern.sub("", mac)
def _bits_left(mac_str):
# Gets the number of bits left in a mac string
return 48 - 4 * len(mac_str)
def search(mac, maximum=1):
global _masks
"""Search for multiple Vendor tuples possibly matching a MAC address.
Args:
mac (str): MAC address in standard format.
maximum (int): Maximum results to return. Defaults to 1.
Returns:
List of Vendor namedtuples containing (manuf, comment), with closest result first. May
be empty if no results found.
Raises:
ValueError: If the MAC could not be parsed.
"""
vendors = []
if maximum <= 0:
return vendors
mac_str = _strip_mac(mac)
mac_int = _get_mac_int(mac_str)
# If the user only gave us X bits, check X bits. No partial matching!
for mask in range(_bits_left(mac_str), 48):
result = _masks.get((mask, mac_int >> mask))
if result:
vendors.append(result)
if len(vendors) >= maximum:
break
return vendors
def get_all(mac):
"""Get a Vendor tuple containing (manuf, comment) from a MAC address.
Args:
mac (str): MAC address in standard format.
Returns:
Vendor: Vendor namedtuple containing (manuf, comment). Either or both may be None if
not found.
Raises:
ValueError: If the MAC could not be parsed.
"""
vendors = search(mac)
if len(vendors) == 0:
return Vendor(manuf=None, manuf_long=None, comment=None)
return vendors[0]
def get_manuf(mac):
"""Returns manufacturer from a MAC address.
Args:
mac (str): MAC address in standard format.
Returns:
string: String containing manufacturer, or None if not found.
Raises:
ValueError: If the MAC could not be parsed.
"""
return get_all(mac).manuf
def get_manuf_long(mac):
"""Returns manufacturer long name from a MAC address.
Args:
mac (str): MAC address in standard format.
Returns:
string: String containing manufacturer, or None if not found.
Raises:
ValueError: If the MAC could not be parsed.
"""
return get_all(mac).manuf_long
def get_comment(mac):
"""Returns comment from a MAC address.
Args:
mac (str): MAC address in standard format.
Returns:
string: String containing comment, or None if not found.
Raises:
ValueError: If the MAC could not be parsed.
"""
return get_all(mac).comment
# Example
#if __name__ == "__main__":
# refresh()
# vendor=get_all('f8:38:80:ed:9d:e8'.upper())
# print(vendor)