Skip to content

Commit b5940b2

Browse files
committed
[change] Add IP to neighbor information
1 parent 1800332 commit b5940b2

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

netengine/backends/snmp/base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import binascii
99
import logging
1010

11+
import netaddr
12+
1113
from netengine.backends import BaseBackend
1214
from netengine.exceptions import NetEngineError
1315

@@ -57,6 +59,20 @@ def _octet_to_mac(self, octet_mac):
5759
)
5860
return mac_address
5961

62+
def _ascii_blocks_to_ipv6(self, ascii_string):
63+
"""
64+
converts an ascii representation into ipv6 address
65+
"""
66+
blocks = ascii_string.split('.')
67+
for b in range(len(blocks)):
68+
blocks[b] = format(int(blocks[b]), '02x')
69+
res = netaddr.IPAddress(
70+
':'.join(
71+
[''.join(blocks[idx : idx + 2]) for idx in range(0, len(blocks), 2)]
72+
)
73+
)
74+
return res
75+
6076
def _oid(self, oid):
6177
"""
6278
returns valid oid value to be passed to getCmd() or nextCmd()

netengine/backends/snmp/openwrt.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ def neighbors(self):
506506
result = []
507507

508508
for index, neighbor in enumerate(neighbors):
509+
oid = neighbor[0][0].getOid()
510+
if oid[12] == 4:
511+
ip = oid[13:]
512+
else:
513+
ip = self._ascii_blocks_to_ipv6(str(oid[13:]))
509514
try:
510515
mac = EUI(
511516
int(neighbor[0][1].prettyPrint(), 16), dialect=mac_unix_expanded
@@ -517,7 +522,12 @@ def neighbors(self):
517522
continue
518523
result.append(
519524
self._dict(
520-
{'mac': str(mac), 'state': str(state), 'interface': str(interface)}
525+
{
526+
'mac': str(mac),
527+
'state': str(state),
528+
'interface': str(interface),
529+
'ip': str(ip),
530+
}
521531
)
522532
)
523533
return result

0 commit comments

Comments
 (0)