Created
October 17, 2014 03:45
-
-
Save fuzzy/67379ff8756bc474043d to your computer and use it in GitHub Desktop.
OUI lookup (requires inet access)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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