/
oui.py
53 lines (40 loc) · 1.21 KB
/
oui.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
import sys
import csv
import functools
csv_filename = "oui.csv"
def read_csv(infilename):
with open(infilename, "r") as infile:
reader = csv.reader(infile)
# skip the first line which should be the header
header = next( reader)
if header[0] != "Registry":
raise ValueError("Invalid OUI CSV file \"%s\"" % infilename)
yield from reader
@functools.lru_cache(maxsize=256)
def find(oui):
reader = read_csv(csv_filename)
for row in reader:
if row[1] == oui:
return row
return None
def parse(macaddr):
# search a macaddr for the OUI
# handle : or - or nothing as separators
# strip quotes so can be compatible with json input
macaddr = macaddr.replace('"','').replace("'","").upper()
if ":" in macaddr:
macaddr = macaddr.replace(":","")
elif "-" in macaddr:
macaddr = macaddr.replace("-","")
# TODO un-do private macaddr
return macaddr[0:6]
def main():
for macaddr in sys.argv[1:]:
oui = parse(macaddr)
info = find(oui)
if not info:
print("mac=\"%s\" unknown" % macaddr)
else:
print(info[2:])
if __name__ == '__main__':
main()