From b5940b2f7928cf695e35ec0444f2da3942ab9c8c Mon Sep 17 00:00:00 2001 From: purhan Date: Mon, 5 Jul 2021 22:31:21 +0530 Subject: [PATCH] [change] Add IP to neighbor information --- netengine/backends/snmp/base.py | 16 ++++++++++++++++ netengine/backends/snmp/openwrt.py | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/netengine/backends/snmp/base.py b/netengine/backends/snmp/base.py index c80f440..4d094dd 100644 --- a/netengine/backends/snmp/base.py +++ b/netengine/backends/snmp/base.py @@ -8,6 +8,8 @@ import binascii import logging +import netaddr + from netengine.backends import BaseBackend from netengine.exceptions import NetEngineError @@ -57,6 +59,20 @@ def _octet_to_mac(self, octet_mac): ) return mac_address + def _ascii_blocks_to_ipv6(self, ascii_string): + """ + converts an ascii representation into ipv6 address + """ + blocks = ascii_string.split('.') + for b in range(len(blocks)): + blocks[b] = format(int(blocks[b]), '02x') + res = netaddr.IPAddress( + ':'.join( + [''.join(blocks[idx : idx + 2]) for idx in range(0, len(blocks), 2)] + ) + ) + return res + def _oid(self, oid): """ returns valid oid value to be passed to getCmd() or nextCmd() diff --git a/netengine/backends/snmp/openwrt.py b/netengine/backends/snmp/openwrt.py index 2bb8616..5711e4e 100644 --- a/netengine/backends/snmp/openwrt.py +++ b/netengine/backends/snmp/openwrt.py @@ -506,6 +506,11 @@ def neighbors(self): result = [] for index, neighbor in enumerate(neighbors): + oid = neighbor[0][0].getOid() + if oid[12] == 4: + ip = oid[13:] + else: + ip = self._ascii_blocks_to_ipv6(str(oid[13:])) try: mac = EUI( int(neighbor[0][1].prettyPrint(), 16), dialect=mac_unix_expanded @@ -517,7 +522,12 @@ def neighbors(self): continue result.append( self._dict( - {'mac': str(mac), 'state': str(state), 'interface': str(interface)} + { + 'mac': str(mac), + 'state': str(state), + 'interface': str(interface), + 'ip': str(ip), + } ) ) return result