blob: ca78c019af4b9de8e440ea063559f6a6b0e01de2 [file] [log] [blame]
Srikanth Vavilapalli1725e492014-12-01 17:50:52 -08001#!/usr/bin/python
2#
3# Copyright (c) 2010,2011,2012,2013 Big Switch Networks, Inc.
4#
5# Licensed under the Eclipse Public License, Version 1.0 (the
6# "License"); you may not use this file except in compliance with the
7# License. You may obtain a copy of the License at
8#
9# http://www.eclipse.org/legal/epl-v10.html
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied. See the License for the specific language governing
15# permissions and limitations under the License.
16#
17
18#
19
20#
21# vendor.py - parse OUI information and look up vendors
22
23import re
24import os
25from pkg_resources import resource_stream
26
27class VendorDB():
28
29 vendors = {}
30
31 def init(self):
32 f = resource_stream(__name__, 'data/oui.txt')
33 if f is None:
34 p = os.path.join(os.path.dirname(__file__), "data", "oui.txt")
35 if not os.path.exists(p):
36 print "Warning: Vendor OUI file could not be located!"
37 return
38 f = open(p)
39
40 while True:
41 l = f.readline()
42 if len(l) ==0:
43 break # EOF
44 if l.count("(base 16)"):
45 oui = l[0:6]
46 vendor = l[22:]
47 self.add_vendor(oui,vendor)
48
49 def add_vendor(self, oui, vendor):
50 oui = oui.strip()
51 vendor = vendor.strip()
52 if len(oui) == 6 and len(vendor) > 1:
53 self.vendors[oui]=vendor
54
55 def get_vendor(self, mac):
56 if len(mac) < 6:
57 return "unknown"
58 mac = mac.upper() # To upper case
59 mac = re.sub(':','',mac) # filter any ":"
60 mac = mac[0:6]
61
62 # Strip broadcast and local bit
63 b = hex(int(mac[1:2],16) & int('1100',2))[2:3]
64 mac = mac[0:1]+b.upper()+mac[2:6]
65
66 if mac in self.vendors:
67 return self.vendors[mac]
68 else:
69 return "Unknown"
70
71# Show all records
72
73if __name__ == '__main__':
74 db = VendorDB()
75 db.init()
76 print db.vendors