Advertisement
DeaD_EyE

mac2vendor from wireshark db

Jun 19th, 2022
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. import pickle
  5. from binascii import unhexlify
  6. from pathlib import Path
  7. from urllib.request import urlopen
  8.  
  9. mac2vendor_file = Path.home() / ".config/mac2vendor.pickle"
  10.  
  11.  
  12. def get_mac2vendor() -> dict[bytes, str]:
  13.     if mac2vendor_file.exists():
  14.         with mac2vendor_file.open("rb") as fd:
  15.             return pickle.load(fd)
  16.  
  17.     url = "https://gitlab.com/wireshark/wireshark/-/raw/master/manuf"
  18.     lines = urlopen(url).read().decode().splitlines()
  19.     mac2vendor = {}
  20.  
  21.     for row in lines:
  22.         if row.startswith("#") or not row.strip():
  23.             continue
  24.  
  25.         mac, *_, company = row.strip().split("\t")
  26.         mac = unhexlify(mac.replace(":", "")[:6])
  27.         mac2vendor[mac] = company
  28.  
  29.     with mac2vendor_file.open("wb") as fd:
  30.         pickle.dump(mac2vendor, fd)
  31.  
  32.     return mac2vendor
  33.  
  34.  
  35. def search(mac: str) -> str | None:
  36.     find_mac = unhexlify(mac.replace(":", "").replace("-", ""))
  37.     for mac_db, name in get_mac2vendor().items():
  38.         if find_mac.startswith(mac_db):
  39.             return name
  40.  
  41.     return None
  42.  
  43.  
  44. print(search("3c:7c:3f:c2:00:00"))
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement