Skip to content

Instantly share code, notes, and snippets.

@flagranterror
Created April 10, 2014 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save flagranterror/10407594 to your computer and use it in GitHub Desktop.
Save flagranterror/10407594 to your computer and use it in GitHub Desktop.
Handy OUI lookup script reworked for Python 3+ and (hopefully) all platforms
#!/usr/bin/python
"""
Who owns the OUI? IEEE knows.
Auto-refreshes once a month.
Run with -u to force update.
"""
from urllib.request import urlopen
from getpass import getuser
import sys
import time
import os
import re
USER_HOME = "~" + getuser()
OUI_FILE_STORE = os.path.join(os.path.expanduser(USER_HOME), ".oui-cache")
CACHE_TIME = 2592000 # 30 days should be fine
IEEE_URL = "http://standards.ieee.org/develop/regauth/oui/oui.txt"
def clean_input(user_input):
"""
Strip colons
"""
return "".join(user_input.split(':')[:3])
def update_cache():
"""
Update our local file from the IEEE OUI
list
"""
print(">>> Updating Cache")
with open(OUI_FILE_STORE, 'wb') as outfile:
for lne in urlopen(IEEE_URL).readlines():
outfile.write(lne)
print(">>> Done")
def process_args():
"""
Parse our args
"""
if sys.argv[1] == "-u":
update_cache()
if len(sys.argv) > 2:
user_input = sys.argv[2]
else:
sys.exit(0)
else:
user_input = sys.argv[1]
return clean_input(user_input)
CLEANED_USER_INPUT = process_args()
try:
if time.time() - os.stat(OUI_FILE_STORE).st_ctime > CACHE_TIME:
update_cache()
except OSError as err:
if err.errno == 2:
update_cache()
with open(OUI_FILE_STORE, 'r',encoding="utf-8") as oui_list:
for line in iter(oui_list):
if re.search(CLEANED_USER_INPUT, line, re.IGNORECASE):
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment