Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created October 17, 2014 03:45
Show Gist options
  • Save fuzzy/67379ff8756bc474043d to your computer and use it in GitHub Desktop.
Save fuzzy/67379ff8756bc474043d to your computer and use it in GitHub Desktop.
OUI lookup (requires inet access)
#!/usr/bin/env python
import re
import sys
import urllib2
import HTMLParser
uri = 'http://aruljohn.com/mac'
headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
ouiTag = None
class HtParse(HTMLParser.HTMLParser):
def qcheck(self, var):
try:
if eval(var):
return True
except:
return False
def handle_starttag(self, tag, attrs):
if tag == 'table':
# See if we are the content-table class
for bit in attrs:
if bit[0] == 'class' and bit[1] == 'content-table':
self.tableFlag = True
elif tag == 'td' and self.qcheck('self.tableFlag'):
self.dataOne = True
def handle_endtag(self, tag):
if tag == 'table' and self.qcheck('self.tableFlag'):
self.tableFlag = False
def handle_data(self, data):
global ouiTag # Ensure we've got this data, I hate globals but hey.
if self.qcheck('self.tableFlag'):
if self.qcheck('self.dataOne') and not self.qcheck('self.dataTwo'):
if data.find(ouiTag):
self.dataOne = False
self.dataTwo = True
if self.qcheck('self.dataOne') and self.qcheck('self.dataTwo'):
print data
self.dataTwo = False
for pos in range(1, len(sys.argv)):
mac = re.sub(':', '', sys.argv[pos]).upper()
ouiTag = sys.argv[pos][0:8]
request = urllib2.Request('%s/%s' % (uri, mac), None, headers)
response = urllib2.urlopen(request)
parser = HtParse()
parser.feed(response.read())
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment