Skip to content

Commit

Permalink
[change] Add IP to neighbor information
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jul 5, 2021
1 parent 1800332 commit b5940b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 16 additions & 0 deletions netengine/backends/snmp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import binascii
import logging

import netaddr

from netengine.backends import BaseBackend
from netengine.exceptions import NetEngineError

Expand Down Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion netengine/backends/snmp/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b5940b2

Please sign in to comment.