#!/usr/bin/env python # OUI IAB # # Douglas Thrift # # $Id$ from __init__ import lock from ccscslab.laptops.models import IAB, OUI import optparse import os import re if __name__ == '__main__': parser = optparse.OptionParser() parser.add_option('-D', '--debug', action = 'store_true', dest = 'debug') options = parser.parse_args()[0] lock(options.debug) data = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data')) oui_ = re.compile(r'^([0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2})\s+\(hex\)\s+(.*)$') with open(os.path.join(data, 'oui.txt'), 'rb') as file: for line in file: line = line.rstrip() match = oui_.match(line) if match is not None: prefix = ':'.join(match.group(1).split('-')) try: # XXX: this is very sketchy company = match.group(2).decode('utf8') except UnicodeDecodeError: company = match.group(2).decode('latin_1') if options.debug: print prefix + ':\n ' + company oui, inserted = OUI.objects.get_or_create(prefix = prefix + ':00:00:00') oui.company = company oui.save() if options.debug: print ' ' + ('inserted' if inserted else 'updated') iab_ = re.compile(r'^([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})-([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})\s+\(base 16\)\s+.*$') with open(os.path.join(data, 'iab.txt'), 'rb') as file: for line in file: line = line.rstrip() match = oui_.match(line) if match is not None: prefix = ':'.join(match.group(1).split('-')) try: # XXX: this is very sketchy company = match.group(2).decode('utf8') except UnicodeDecodeError: company = match.group(2).decode('latin_1') oui = OUI.objects.get(prefix = prefix + ':00:00:00') else: match = iab_.match(line) if match is not None: lower = ':'.join(match.group(1, 2, 3)) upper = ':'.join(match.group(4, 5, 6)) if options.debug: print prefix + ':\n ' + company + '\n ' + lower + '\n ' + upper iab, inserted = IAB.objects.get_or_create(oui = oui, lower = prefix + ':' + lower, upper = prefix + ':' + upper) iab.company = company iab.save() if options.debug: print ' ' + ('inserted' if inserted else 'updated')