Skip to content

Instantly share code, notes, and snippets.

@stintel
Last active September 20, 2023 12:13
Show Gist options
  • Save stintel/b03ae733492acf7df72408a91508eac7 to your computer and use it in GitHub Desktop.
Save stintel/b03ae733492acf7df72408a91508eac7 to your computer and use it in GitHub Desktop.
bin/mac.py
#!/usr/bin/env python
from string import hexdigits
from sys import argv
OUIDB = '/usr/share/nmap/nmap-mac-prefixes'
def fmt_mac_three(m):
return f"{m[:4]}-{m[4:8]}-{m[8:13]}"
def fmt_mac_six(m):
return f"{m[:2]}:{m[2:4]}:{m[4:6]}:{m[6:8]}:{m[8:10]}:{m[10:12]}"
smac = ''
for c in argv[1]:
if c in hexdigits:
smac += c
bmac = bytes.fromhex(smac)
print(f"MAC address {smac} {fmt_mac_three(smac)} {fmt_mac_six(smac)}")
if int.from_bytes(bmac[:1], byteorder='big') & 1 << 1:
if int.from_bytes(bmac[:1], byteorder='big') & 1 << 0:
print("Locally administered multicast address")
else:
print("Locally administered unicast address")
else:
with open(OUIDB) as f:
d = dict(x.rstrip().split(None, 1) for x in f)
try:
print(d[''.join(smac.upper())[:6]])
except :
print("Not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment