summaryrefslogtreecommitdiffstats
path: root/scripts/oui-parser.py
blob: e34049318a8958c4ec2f5014920bd37dcbc90d2c (plain)
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
#!/usr/bin/python3

# This is free software, licensed under the CC0 1.0 Universal.
# See /LICENSE for more information.

import re
import json
import requests as req
import sys
import os


def parse_ieee_file():
    vendors = {}

    with open(f"./oui.txt", "r") as f:
        ouitext = f.read()
        for line in ouitext.splitlines():
            match = re.match(r'^([0-9A-Fa-f]{6})\s+\(base 16\)\s+(.+)$', line)

            if match:
                oui = match.group(1).lower()
                mac_oui = "mac-oui-{}|1".format(oui)
                vendor = match.group(2).strip()

                if vendor not in vendors:
                    vendors[vendor] = []

                vendors[vendor].append(mac_oui)

    entry = {
        "vendor=%": vendors
    }

    with open(f"./modules/oui.json", "w") as f:
        json.dump(entry, f, indent=4)


def download_ieee_file():
    url = "https://standards-oui.ieee.org"

    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/',
    }

    r = req.get(url, headers=headers)

    with open(f"./oui.txt", "w") as f:
        f.write(r.text)


if sys.argv[1] == "download":
    download_ieee_file()

if sys.argv[1] == "parse":
    parse_ieee_file()