Skip to content

Instantly share code, notes, and snippets.

@yosignals
Last active August 30, 2023 08:28
Show Gist options
  • Save yosignals/36ce520a78a9526ef0d5a05156eb4bf1 to your computer and use it in GitHub Desktop.
Save yosignals/36ce520a78a9526ef0d5a05156eb4bf1 to your computer and use it in GitHub Desktop.
Pulls down the Organizationally Unique Identifier's (OUI), cross references, joins and summarises groups of Mac addresses to the vendor, highlighting volume in percentages in a summary - great for wild asset things
import urllib.request as urllib2
import re
from collections import Counter
import csv
def ParseIEEEOui(url="http://standards.ieee.org/develop/regauth/oui/oui.txt"):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'}
req = urllib2.Request(url, headers=headers)
res = urllib2.urlopen(req)
data = res.read().decode()
IEEOUI = {}
for line in data.split('\n'):
try:
mac, company = re.search(r'([0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2})\s+\(hex\)\s+(.+)', line).groups()
IEEOUI[mac.replace('-', '').upper()] = company
except AttributeError:
continue
return IEEOUI
# Load OUI data from the IEEE website
oui_data = ParseIEEEOui()
# Load your MAC addresses
with open('your_macs.txt', 'r') as f:
macs = f.readlines()
# Initialize a list to hold vendors, OUIs and full MACs
output_data = []
# Process and populate output_data
for mac in macs:
clean_mac = mac.replace(':', '').replace('-', '').replace(';', '').strip().upper()
oui = clean_mac[:6]
full_mac = mac.strip()
vendor = oui_data.get(oui, "Unknown")
output_data.append([vendor, oui, full_mac])
# Write output to CSV
csv_file = 'output.csv'
with open(csv_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["Vendor", "OUI", "Full MAC"])
writer.writerows(output_data)
# Generate summary
vendor_counter = Counter()
total_macs = 0
with open(csv_file, 'r', newline='') as f:
reader = csv.reader(f)
next(reader) # skip header
for row in reader:
vendor = row[0]
vendor_counter[vendor] += 1
total_macs += 1
# Output summary information to a text file
with open('summary.txt', 'w') as f:
f.write("Summary:\n")
for vendor, count in vendor_counter.items():
percentage = (count / total_macs) * 100
f.write(f"Vendor: {vendor}, Total MAC Addresses: {count}, Percentage: {percentage:.2f}%\n")
# Also print the summary to the console
print("Summary:")
for vendor, count in vendor_counter.items():
percentage = (count / total_macs) * 100
print(f"Vendor: {vendor}, Total MAC Addresses: {count}, Percentage: {percentage:.2f}%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment