Commit a619d5cc authored by Franco Fichtner's avatar Franco Fichtner

Merge pull request #696 from gitdevmod/lookup-mac

use netaddr to lookup mac address in list_arp.py and list_ndp.py
parents c45bb6cf 62bccbbe
...@@ -97,6 +97,7 @@ CORE_DEPENDS?= apinger \ ...@@ -97,6 +97,7 @@ CORE_DEPENDS?= apinger \
py27-requests \ py27-requests \
py27-sqlite3 \ py27-sqlite3 \
py27-ujson \ py27-ujson \
py27-netaddr \
python27 \ python27 \
radvd \ radvd \
rate \ rate \
......
...@@ -34,17 +34,11 @@ import os ...@@ -34,17 +34,11 @@ import os
import os.path import os.path
import sys import sys
import ujson import ujson
from netaddr import *
if __name__ == '__main__': if __name__ == '__main__':
result = [] result = []
# import mac manufacturer mac address table (index by mac prefix)
mac_table = {}
if os.path.isfile('/usr/local/share/nmap/nmap-mac-prefixes'):
for mac in open('/usr/local/share/nmap/nmap-mac-prefixes').read().split('\n'):
if len(mac) > 8:
mac_table[mac[0:6]] = mac[7:]
# import dhcp_leases (index by ip address) # import dhcp_leases (index by ip address)
dhcp_leases = {} dhcp_leases = {}
dhcp_leases_filename = '/var/dhcpd/var/db/dhcpd.leases' dhcp_leases_filename = '/var/dhcpd/var/db/dhcpd.leases'
...@@ -70,9 +64,10 @@ if __name__ == '__main__': ...@@ -70,9 +64,10 @@ if __name__ == '__main__':
'manufacturer': '', 'manufacturer': '',
'hostname': '' 'hostname': ''
} }
manufacturer_mac = record['mac'].replace(':' ,'')[:6].upper() manufacturer_mac = EUI(record['mac'])
if manufacturer_mac in mac_table: oui = manufacturer_mac.oui
record['manufacturer'] = mac_table[manufacturer_mac] if oui.registration().org:
record['manufacturer'] = oui.registration().org
if record['ip'] in dhcp_leases: if record['ip'] in dhcp_leases:
record['hostname'] = dhcp_leases[record['ip']]['hostname'] record['hostname'] = dhcp_leases[record['ip']]['hostname']
result.append(record) result.append(record)
......
...@@ -34,17 +34,11 @@ import os ...@@ -34,17 +34,11 @@ import os
import os.path import os.path
import sys import sys
import ujson import ujson
from netaddr import *
if __name__ == '__main__': if __name__ == '__main__':
result = [] result = []
# import mac manufacturer mac address table (index by mac prefix)
mac_table = {}
if os.path.isfile('/usr/local/share/nmap/nmap-mac-prefixes'):
for mac in open('/usr/local/share/nmap/nmap-mac-prefixes').read().split('\n'):
if len(mac) > 8:
mac_table[mac[0:6]] = mac[7:]
# parse ndp output # parse ndp output
with tempfile.NamedTemporaryFile() as output_stream: with tempfile.NamedTemporaryFile() as output_stream:
subprocess.call(['/usr/sbin/ndp', '-an'], stdout=output_stream, stderr=open(os.devnull, 'wb')) subprocess.call(['/usr/sbin/ndp', '-an'], stdout=output_stream, stderr=open(os.devnull, 'wb'))
...@@ -58,9 +52,10 @@ if __name__ == '__main__': ...@@ -58,9 +52,10 @@ if __name__ == '__main__':
'intf': line_parts[2], 'intf': line_parts[2],
'manufacturer': '' 'manufacturer': ''
} }
manufacturer_mac = record['mac'].replace(':' ,'')[:6].upper() manufacturer_mac = EUI(record['mac'])
if manufacturer_mac in mac_table: oui = manufacturer_mac.oui
record['manufacturer'] = mac_table[manufacturer_mac] if oui.registration().org:
record['manufacturer'] = oui.registration().org
result.append(record) result.append(record)
# handle command line argument (type selection) # handle command line argument (type selection)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment