def cisco_ios(IP, COMMUNITY): from puresnmp import table from puresnmp import get import pandas as pd oid_name = "1.3.6.1.4.1.9.9.23.1.3.4.0" oid_if = "1.3.6.1.4.1.9.9.23.1.1.1.1" oid_cdp = "1.3.6.1.4.1.9.9.23.1.2.1" column_names = [ "local", "local_if", "remote", "remote_if", "remote_ip", "remote_model" ] df_result = pd.DataFrame(columns=column_names) try: local_name = get(IP, COMMUNITY, oid_name) local_name = local_name.decode() interfaces = table(IP, COMMUNITY, oid_if) neighbors = table(IP, COMMUNITY, oid_cdp) for neighbor in neighbors: interface = interfaces[0] new_row = { "local": local_name, "local_if": interface[str(int(float(neighbor['0'])))].decode(), "remote": neighbor['6'].decode(), "remote_if": neighbor['7'].decode(), "remote_ip": iphex(neighbor['4'].hex()), "remote_model": neighbor['8'].decode() } df_result = df_result.append(new_row, ignore_index=True) return df_result except: #print ("error en: " + IP) return df_result
def test_table(self, mck_rid, mck_tablify, mck_walk): mck_rid.return_value = 0 tmp = object() # dummy return value mck_walk.return_value = tmp table('::1', 'public', '1.2.3.4', port=161, num_base_nodes=2) mck_walk.assert_called_with('::1', 'public', '1.2.3.4', port=161) mck_tablify.assert_called_with(tmp, num_base_nodes=2)
def test_table(self): responses = readbytes_multiple('apiv1/table_response.hex') self.transport.send.side_effect = responses result = snmp.table('127.0.0.1', 'private', '1.3.6.1.2.1.2.2') # Sort the result for Python < 3.6 (unsorted dicts) result = sorted(result, key=lambda item: item['0']) expected = [{ '0': '1', '1': 1, '10': 172, '11': 2, '12': 0, '13': 0, '14': 0, '15': 0, '16': 172, '17': 2, '18': 0, '19': 0, '2': b'lo', '20': 0, '21': 0, '22': '0.0', '3': 24, '4': 65536, '5': 10000000, '6': b'', '7': 1, '8': 1, '9': datetime.timedelta(0) }, { '0': '12', '1': 12, '10': 13952, '11': 140, '12': 0, '13': 0, '14': 0, '15': 0, '16': 4391, '17': 51, '18': 0, '19': 0, '2': b'eth0', '20': 0, '21': 0, '22': '0.0', '3': 6, '4': 1500, '5': 4294967295, '6': b'\x02B\xac\x11\x00\x02', '7': 1, '8': 1, '9': datetime.timedelta(0) }] self.assertEqual(result, expected) self.assertEqual(self.transport.send.call_count, 45) for row in result: self.assertIsInstance(row, dict) dict_types = {type(key) for key in row.keys()} if six.PY3: self.assertEqual(dict_types, {str}) else: self.assertEqual(dict_types, {unicode})
def cisco_sg300(IP, COMMUNITY): from puresnmp import walk from puresnmp import get from puresnmp import table import pandas as pd oid_name = "1.3.6.1.4.1.9.9.23.1.3.4.0" oid_if = "1.3.6.1.4.1.9.9.23.1.1.1.1" oid_cdp = "1.3.6.1.4.1.9.9.23.1.2.1" oid_if_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.7" oid_ip_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.4" oid_name_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.6" oid_model_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.8" try: local_name = get(IP, COMMUNITY, oid_name) local_name = local_name.decode() interfaces = table(IP, COMMUNITY, oid_if) interfaces = interfaces[0] remote = [] local_if = [] for row in walk(IP, COMMUNITY, oid_name_remote): interface = str(row[0]) interface = interface.split(".") port = interfaces[interface[14]] remote.append(row[1].decode()) local_if.append(port.decode()) df_result = pd.DataFrame({ 'local': local_name, 'local_if': local_if, 'remote': remote }) ## interface remota result_remote_if = table(IP, COMMUNITY, oid_if_remote) result_remote_if = result_remote_if[0] del result_remote_if['0'] remote_if = list(result_remote_if.values()) remote_if = [i.decode() for i in remote_if] df_result['remote_if'] = remote_if ### ips remote ips = table(IP, COMMUNITY, oid_ip_remote) ips = ips[0] del ips['0'] ips = list(ips.values()) ips = [i.hex() for i in ips] ips = [iphex(i) for i in ips] df_result['remote_ip'] = ips ### remote model model_remote = table(IP, COMMUNITY, oid_model_remote) model_remote = model_remote[0] del model_remote['0'] model_remote = list(model_remote.values()) model_remote = [i.decode() for i in model_remote] df_result['remote_model'] = model_remote return df_result except: print("error : " + IP) column_names = [ "local", "local_if", "remote", "remote_if", "remote_ip", "remote_model" ] df_result = pd.DataFrame(columns=column_names) return df_result
def __process_methods(method, common_parameters, datatype_config): response = None if method == "get": oid = datatype_config["oid"] response = puresnmp.get(**common_parameters, oid=oid) elif method == "multiget": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) response = puresnmp.multiget(**common_parameters, oids=oids) elif method == "getnext": oid = datatype_config["oid"] master_response = puresnmp.getnext(**common_parameters, oid=oid) response = {master_response.oid: master_response.value} elif method == "multigetnext": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) master_response = puresnmp.multigetnext(**common_parameters, oids=oids) response = { binded_var.oid: binded_var.value for binded_var in master_response } elif method == "walk": oid = datatype_config["oid"] response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.walk(**common_parameters, oid=oid)) } elif method == "multiwalk": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.multiwalk(**common_parameters, oids=oids)) } elif method == "set": oid = datatype_config["oid"] value = datatype_config["value"] response = puresnmp.set(**common_parameters, oid=oid, value=value) elif method == "multiset": mappings = datatype_config["mappings"] response = puresnmp.multiset(**common_parameters, mappings=mappings) elif method == "bulkget": scalar_oids = datatype_config.get("scalarOid", []) scalar_oids = scalar_oids if isinstance( scalar_oids, list) else list(scalar_oids) repeating_oids = datatype_config.get("repeatingOid", []) repeating_oids = repeating_oids if isinstance( repeating_oids, list) else list(repeating_oids) max_list_size = datatype_config.get("maxListSize", 1) response = puresnmp.bulkget(**common_parameters, scalar_oids=scalar_oids, repeating_oids=repeating_oids, max_list_size=max_list_size)._asdict() elif method == "bulkwalk": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) bulk_size = datatype_config.get("bulkSize", 10) response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.bulkwalk( **common_parameters, bulk_size=bulk_size, oids=oids)) } elif method == "table": oid = datatype_config["oid"] del common_parameters["timeout"] num_base_nodes = datatype_config.get("numBaseNodes", 0) response = puresnmp.table(**common_parameters, oid=oid, num_base_nodes=num_base_nodes) elif method == "bulktable": oid = datatype_config["oid"] num_base_nodes = datatype_config.get("numBaseNodes", 0) bulk_size = datatype_config.get("bulkSize", 10) response = puresnmp.bulktable(**common_parameters, oid=oid, num_base_nodes=num_base_nodes, bulk_size=bulk_size) else: log.error("Method \"%s\" - Not found", str(method)) return response