diff --git a/netengine/backends/snmp/airos.py b/netengine/backends/snmp/airos.py index fce0844..e194c5d 100644 --- a/netengine/backends/snmp/airos.py +++ b/netengine/backends/snmp/airos.py @@ -401,9 +401,9 @@ def to_dict(self): return self._dict( { 'type': 'DeviceMonitoring', - 'general': {'uptime': self.uptime,}, + 'general': {'uptime': self.uptime}, 'resources': { - 'memory': {'total': self.RAM_total, 'free': self.RAM_free,}, + 'memory': {'total': self.RAM_total, 'free': self.RAM_free}, }, 'interfaces': self.interfaces_to_dict, } diff --git a/netengine/backends/snmp/base.py b/netengine/backends/snmp/base.py index 0dbc129..c80f440 100644 --- a/netengine/backends/snmp/base.py +++ b/netengine/backends/snmp/base.py @@ -51,7 +51,7 @@ def _octet_to_mac(self, octet_mac): returns a valid mac address for a given octetstring """ mac_address = binascii.b2a_hex(octet_mac.encode()).decode() - if mac_address is not '': + if mac_address != '': mac_address = ':'.join( [mac_address[slice(i, i + 2)] for i in range(0, 12, 2) if i != ''] ) diff --git a/netengine/backends/snmp/openwrt.py b/netengine/backends/snmp/openwrt.py index 557d5b7..8de89dd 100644 --- a/netengine/backends/snmp/openwrt.py +++ b/netengine/backends/snmp/openwrt.py @@ -11,6 +11,7 @@ from datetime import timedelta import pytz +from netaddr import EUI, mac_unix_expanded from netengine.backends.snmp import SNMP @@ -480,7 +481,7 @@ def resources_to_dict(self): 'free': self.RAM_free, 'cached': self.RAM_cached, }, - 'swap': {'total': self.SWAP_total, 'free': self.SWAP_free,}, + 'swap': {'total': self.SWAP_total, 'free': self.SWAP_free}, } ) return result @@ -505,14 +506,14 @@ def neighbors(self): neighbor_states = self.next('1.3.6.1.2.1.4.35.1.7')[3] result = [] - for i in range(len(neighbors)): - interface_num = neighbors[i][0][0].getOid()[10] + for index, neighbor in enumerate(neighbors): + mac = EUI(int(neighbor[0][1].prettyPrint(), 16), dialect=mac_unix_expanded) + interface_num = neighbor[0][0].getOid()[10] interface = self.get(f'1.3.6.1.2.1.31.1.1.1.1.{interface_num}')[3][0][1] - state = states_map[str(neighbor_states[i][0][1])] - mac = self._octet_to_mac(str(neighbors[i][0][1])) + state = states_map[str(neighbor_states[index][0][1])] result.append( self._dict( - {'mac': str(mac), 'state': str(state), 'interface': str(interface),} + {'mac': str(mac), 'state': str(state), 'interface': str(interface)} ) ) return result diff --git a/tests/static/test-openwrt-snmp-oid.json b/tests/static/test-openwrt-snmp-oid.json index 94da882..480de5c 100644 --- a/tests/static/test-openwrt-snmp-oid.json +++ b/tests/static/test-openwrt-snmp-oid.json @@ -51,5 +51,8 @@ "1.3.6.1.2.1.25.2.3.1.6.8": "88", "1.3.6.1.2.1.25.2.3.1.6.10": "0", "1.3.6.1.2.1.25.2.3.1.5.10": "0", - "1.3.6.1.2.1.25.1.2.0": "\u0007\u00e5\u0006\t14\ufffd+\ufffd\ufffd" + "1.3.6.1.2.1.25.1.2.0": { + "type": "bytes", + "value": "\\x07\\xe5\\x06\\x0b\\x06\\x00\r\\x00+\\x00\\x00" + } } diff --git a/tests/test_snmp/test_openwrt.py b/tests/test_snmp/test_openwrt.py index 10cfe1e..40c1725 100644 --- a/tests/test_snmp/test_openwrt.py +++ b/tests/test_snmp/test_openwrt.py @@ -116,6 +116,9 @@ def test_CPU_count(self): def test_neighbors(self): self.assertIsInstance(self.device.neighbors, list) + def test_local_time(self): + self.assertIsInstance(self.device.local_time, int) + def test_to_dict(self): with self.nextcmd_patcher as p: SpyMock._update_patch(p, _mock_return_value=[0, 0, 0, []]) diff --git a/tests/utils.py b/tests/utils.py index aca6018..e6fcac0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,3 +1,4 @@ +import codecs import json import os from unittest import mock @@ -32,7 +33,12 @@ def _load_mock_json(file): def _get_mocked_getcmd(data, input): oid = input[2] result = data[oid] - if type(result) == list: + if type(result) == dict: + _type = result['type'] + _value = result['value'] + if _type == 'bytes': + result = codecs.escape_decode(_value)[0] + elif type(result) == list: result = '\n'.join(result[0:]) return [0, 0, 0, [[0, result]]]