class TestApi: def __init__(self): self.parse_json = JsonParser() def test_method(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) sw_api_call = Switch(cpi_username, cpi_password, cpi_ipv4_address, logger) dev_id = sw_api_call.id_by_ip("172.30.28.246") result = sw_api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'inventoryDetailsDTO', 'ethernetInterfaces', 'ethernetInterface' ] interface_list_of_dicts = self.parse_json.value( result, key_list, logger) for interface_dict in interface_list_of_dicts: for key in interface_dict: # iterating over dict's return keys only if interface_dict[key] == 'GigabitEthernet1/0/1': print(json.dumps(interface_dict, indent=4))
class Find: def __init__(self): self.parse_json = JsonParser() def ip_client(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) dev_id = api_call.id_by_ip(values_dict['address']) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'macAddress' ] mac_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, mac_addr def ip_ap(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): ap_api_call = AccessPoint(cpi_username, cpi_password, cpi_ipv4_address, logger) client_api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) client_id = client_api_call.id_by_ip(values_dict['address']) ap_id = ap_api_call.id_by_ip(values_dict['address']) ap_result = ap_api_call.json_detailed(ap_id) client_result = client_api_call.json_detailed(client_id) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborName' ] neigh_name = self.parse_json.value(ap_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborIpAddress' ] tmp = self.parse_json.value(ap_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborPort' ] interface = self.parse_json.value(ap_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'ethernetMac' ] mac_addr = self.parse_json.value(ap_result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ap mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, vlan, vlan_name, mac_addr def ip_phone(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) dev_id = api_call.id_by_ip(values_dict['address']) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'macAddress' ] mac_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, mac_addr def mac_client(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) dev_id = api_call.id_by_mac(values_dict['address']) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ipAddress', 'address' ] ip_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ip addr :{}".format(ip_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, ip_addr def mac_ap(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): ap_api_call = AccessPoint(cpi_username, cpi_password, cpi_ipv4_address, logger) client_api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) client_id = client_api_call.id_by_mac(values_dict['address']) ap_id = ap_api_call.id_by_mac(values_dict['address']) ap_result = ap_api_call.json_detailed(ap_id) client_result = client_api_call.json_detailed(client_id) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborName' ] neigh_name = self.parse_json.value(ap_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborIpAddress' ] tmp = self.parse_json.value(ap_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborPort' ] interface = self.parse_json.value(ap_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'ipAddress' ] ip_addr = self.parse_json.value(ap_result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ap ip addr :{}".format(ip_addr)) return neigh_name, neigh_ip, interface, vlan, vlan_name, ip_addr def mac_phone(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): api_call = Client(cpi_username, cpi_password, cpi_ipv4_address, logger) dev_id = api_call.id_by_mac(values_dict['address']) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ipAddress', 'address' ] ip_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ip addr :{}".format(ip_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, ip_addr
class Tools: def __init__(self): self.find = Find() self.parse_json = JsonParser() def checkAlarms(self, args, config, logger): # email_string = "" num_successful=0 num_failed=0 alarm_api_call = Alarms(config, logger) device_api_call = AccessPoint(config, logger) crit_list = alarm_api_call.get_critical_alarm_ids() for alarm_id in crit_list: time.sleep(0.3) mac = alarm_api_call.devname_by_alarm_id(alarm_id) dev_id=device_api_call.id_by_alarm_mac_detailed(mac) dev_result = device_api_call.json_detailed(dev_id) #logger.info("------- Matching Switch #{}--------".format(dev_id_list.index(curr_id) + 1)) dev_dict = {} key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'name'] dev_dict['name'] = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'model'] dev_dict['model'] = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'reachabilityStatus'] dev_dict['status'] = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborName'] dev_dict['nb_name'] = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborPort'] dev_dict['nb_port'] = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborIpAddress'] dev_dict['nb_ip'] = self.parse_json.value(dev_result, key_list, logger) logger.info("Processing Alarm {} / {} ID: {} ".format(crit_list.index(alarm_id)+1,len(crit_list),alarm_id)) logger.info("AP: {} Model:{} Status:{}".format(dev_dict['name'], dev_dict['model'], dev_dict['status'])) logger.info("Neighbor:{}({}):{}".format(dev_dict['nb_name'], dev_dict['nb_ip'], dev_dict['nb_port'])) time.sleep(1) # don't test for sync status too soon (CPI delay and all that) if args.toggle: Return = self.ap_reload(args,dev_dict["nb_ip"],dev_dict["nb_port"]) success_string = "Shut/No Shut on {}({}): {}".format(dev_dict['nb_name'], dev_dict['nb_ip'], dev_dict['nb_port']) if Return.returncode == 0: success_string += " Successful" num_successful += 1 alarm_api_call.acknowledge_by_alarm_id(alarm_id) #acknowledge alarm else: success_string += " FAILED" num_failed += 1 logger.info(success_string) # email_string += success_string + "\n" #logger.debug(email_string) logger.info("Total {} Alarms".format(len(crit_list))) logger.info("{} ports successfully reloaded ".format(num_successful)) logger.info("{} ports failed to reload".format(num_failed)) def ap_reload (self,args, ip,port): time.sleep(1) # don't test for sync status too soon (CPI delay and all that) arg_run_list = "dnmt tools ap_poke {} {} ".format(ip, port) if args.batch: arg_run_list += "-s" Return_val = subprocess.run(arg_run_list, shell=True) ###<TODO> EXTERNAL CALL to DNMT return Return_val def un_ack_alarms(self, args, config, logger): alarm_api_call = Alarms(config, logger) crit_list = alarm_api_call.get_acked_critical_alarm_ids() for alarm_id in crit_list: alarm_api_call.unacknowledge_by_alarm_id(alarm_id) #acknowledge alarm def slow_aps(self, args, config, logger): api_call = AccessPoint(config, logger) device_api_call = AccessPoint(config, logger) dev_id_list = device_api_call.get_slow_ports() if len(dev_id_list) < 1: sys.exit(1) for curr_id in dev_id_list: result = api_call.json_detailed(curr_id) logger.info("------- Occurrence #{}--------\n".format(dev_id_list.index(curr_id) + 1)) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor',0,'neighborName'] neigh_name = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor',0,'neighborIpAddress'] neigh_ip = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor',0,'neighborPort'] interface = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor',0,'interfaceSpeed'] speed = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'name'] name = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'model'] model = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'locationHierarchy'] map_location = self.parse_json.value(result, key_list, logger) # logger.info("ap name :{}".format(name)) logger.info("ap model :{}".format(model)) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("speed :{}".format(speed)) logger.info("map location :{}".format(map_location)) if args.toggle: Return = self.ap_reload(args, neigh_ip, interface) success_string = "Shut/No Shut on {}({}): {}".format(neigh_name, neigh_ip,interface) if Return.returncode == 0: success_string += " Successful" else: success_string += " FAILED" logger.info(success_string) #<TODO move this and previous into a function to reuse, add a sync before> # time.sleep(60) # Give the AP some time to start up # result = api_call.json_detailed(curr_id) # logger.info("------- Occurrence #{} POST RELOAD--------\n".format(dev_id_list.index(curr_id) + 1)) # # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, # 'neighborName'] # neigh_name = self.parse_json.value(result, key_list, logger) # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, # 'neighborIpAddress'] # neigh_ip = self.parse_json.value(result, key_list, logger) # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, # 'neighborPort'] # interface = self.parse_json.value(result, key_list, logger) # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, # 'interfaceSpeed'] # speed = self.parse_json.value(result, key_list, logger) # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'name'] # name = self.parse_json.value(result, key_list, logger) # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'model'] # model = self.parse_json.value(result, key_list, logger) # key_list = ['queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'locationHierarchy'] # map_location = self.parse_json.value(result, key_list, logger) # # # logger.info("ap name :{}".format(name)) # logger.info("ap model :{}".format(model)) # logger.info("switch name :{}".format(neigh_name)) # logger.info("switch ip :{}".format(neigh_ip)) # logger.info("interface :{}".format(interface)) # logger.info("speed :{}".format(speed)) # logger.info("map location :{}".format(map_location)) # End reload else: time.sleep(1) # sleep at the end of each to prevent overruns when running without toggle
class Connector: def __init__(self, uname, passwd, cpi_ipv4_addr, log): self.username = uname self.password = passwd self.cpi_ipv4_address = cpi_ipv4_addr self.logger = log self.parse_json = JsonParser() urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # --- Prime job execution and handling def job_complete(self, job_id): url = "https://{}/webacs/api/v3/op/jobService/runhistory.json?jobName=\"{}\"".format( self.cpi_ipv4_address, job_id) result = self.error_handling(requests.get, 5, url, False, self.username, self.password) key_list = [ 'mgmtResponse', 'job', 0, 'runInstances', 'runInstance', 0, 'runStatus' ] status = self.parse_json.value(result.json(), key_list, self.logger) if status == "COMPLETED": # job complete return True elif status == "RUNNING": # job running return False else: # ADD CRITICAL LOGGING HERE print('critical, job not run correctly') sys.exit(1) def job_successful(self, job_id): url = "https://{}/webacs/api/v3/op/jobService/runhistory.json?jobName=\"{}\"".format( self.cpi_ipv4_address, job_id) result = self.error_handling(requests.get, 5, url, False, self.username, self.password) key_list = [ 'mgmtResponse', 'job', 0, 'runInstances', 'runInstance', 0, 'resultStatus' ] status = self.parse_json.value(result.json(), key_list, self.logger) if status == "SUCCESS": return True elif status == "FAILURE": return False # --- end Prime job handling # a decorator-like method for error handling def error_handling(self, api_call_method, base_case, *args): try: if api_call_method == requests.get: # a GET api call req = api_call_method(args[0], verify=args[1], auth=(args[2], args[3])) elif api_call_method == requests.post: # a POST api call req = api_call_method(args[0], verify=args[1], auth=(args[2], args[3]), json=args[4]) elif api_call_method == requests.put: # a PUT api call req = api_call_method(args[0], verify=args[1], auth=(args[2], args[3]), json=args[4]) else: pass req.raise_for_status() except requests.exceptions.HTTPError as error: if req.status_code == 503 or req.status_code == 403: # too many requests at once self.logger.warning( "Too many API calls at once. Slight delay before retrying. Status code: {}" .format(req.status_code)) time.sleep(random.uniform(1.0, 5.0)) base_case -= 1 # decrement base_case by one if base_case == 0: self.logger.critical( "Recursive error handler's base case reached. Abandoning API calls." ) self.logger.critical(api_call_method) self.logger.critical(args[0]) sys.exit(1) self.logger.debug( "Base case just before recursive call: {}".format( base_case)) if api_call_method == requests.get: req = self.error_handling(api_call_method, base_case, args[0], args[1], args[2], args[3]) elif api_call_method == requests.post or api_call_method == requests.put: req = self.error_handling(api_call_method, base_case, args[0], args[1], args[2], args[3], args[4]) else: self.logger.critical("Recursive API call failed") sys.exit(1) elif req.status_code == 401: self.logger.critical("Bad authentication. Check credentials.") self.logger.critical(error) sys.exit(1) else: self.logger.critical("API call failed.") self.logger.critical(error) sys.exit(1) except requests.exceptions.RequestException as e: # catch-all failure, exit program self.logger.critical(e) sys.exit(1) finally: return req # --- print API calls, mainly for testing def test(self): url = "https://{}/webacs/api/v3/data/AccessPoints.json?name=\"ESQ_4-430_5e3c\"" result = self.error_handling(requests.get, 5, url, False, self.username, self.password) print(json.dumps(result.json(), indent=4))
class Find: def __init__(self): self.parse_json = JsonParser() def dev_id(self, args, config, ipaddr, logger): # 400 critical error is thrown if description is not found api_call = Switch(config, logger) # check address to see if hostname or IP if "-" in ipaddr: dev_id = api_call.id_by_hostname(ipaddr.strip()) else: dev_id = api_call.id_by_ip(ipaddr.strip()) return dev_id #def ip_client(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): def ip_client(self, args, config, logger): api_call = Client(config, logger) dev_id = api_call.id_by_ip(args.address) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'macAddress' ] mac_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, mac_addr def ip_ap(self, args, config, logger): ap_api_call = AccessPoint(config, logger) client_api_call = Client(config, logger) client_id = client_api_call.id_by_ip(args.address) ap_id = ap_api_call.id_by_ip(args.address) ap_result = ap_api_call.json_detailed(ap_id) client_result = client_api_call.json_detailed(client_id) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborName' ] neigh_name = self.parse_json.value(ap_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborIpAddress' ] tmp = self.parse_json.value(ap_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborPort' ] interface = self.parse_json.value(ap_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'ethernetMac' ] mac_addr = self.parse_json.value(ap_result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ap mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, vlan, vlan_name, mac_addr def ip_phone(self, args, config, logger): api_call = Client(config, logger) dev_id = api_call.id_by_ip(args.address) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'macAddress' ] mac_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, mac_addr def mac_client(self, args, config, logger): api_call = Client(config, logger) dev_id = api_call.id_by_mac(args.address) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ipAddress', 'address' ] ip_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ip addr :{}".format(ip_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, ip_addr def mac_ap(self, args, config, logger): ap_api_call = AccessPoint(config, logger) client_api_call = Client(config, logger) client_id = client_api_call.id_by_mac(args.address) ap_id = ap_api_call.id_by_mac(args.address) ap_result = ap_api_call.json_detailed(ap_id) client_result = client_api_call.json_detailed(client_id) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborName' ] neigh_name = self.parse_json.value(ap_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborIpAddress' ] tmp = self.parse_json.value(ap_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'cdpNeighbors', 'cdpNeighbor', 0, 'neighborPort' ] interface = self.parse_json.value(ap_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(client_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'accessPointDetailsDTO', 'ipAddress' ] ip_addr = self.parse_json.value(ap_result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ap ip addr :{}".format(ip_addr)) return neigh_name, neigh_ip, interface, vlan, vlan_name, ip_addr def mac_phone(self, args, config, logger): api_call = Client(config, logger) dev_id = api_call.id_by_mac(args.address) result = api_call.json_detailed(dev_id) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan'] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ipAddress', 'address' ] ip_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("ip addr :{}".format(ip_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, ip_addr def desc(self, args, config, logger): # 400 critical error is thrown if description is not found api_call = Switch(config, logger) dev_id_list = api_call.ids_by_desc(args.description.strip('"'), args.name) logger.info( " # of switches with Matching occurrences of \"{}\" found: {} ". format(args.description, len(dev_id_list))) # exit out of loop if no matches if len(dev_id_list) < 1: sys.exit(1) for curr_id in dev_id_list: dev_result = api_call.json_basic( curr_id) # Modify this to go through multiple result = api_call.json_detailed( curr_id) # Modify this to go through multiple logger.info("------- Matching Switch #{}--------".format( dev_id_list.index(curr_id) + 1)) key_list = [ 'queryResponse', 'entity', 0, 'devicesDTO', 'deviceName' ] neigh_name = self.parse_json.value(dev_result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'devicesDTO', 'ipAddress' ] tmp = self.parse_json.value(dev_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible ###########currently working out how to grab items that match values[description]_dict. dev_interfaces = result['queryResponse']['entity'][0][ 'inventoryDetailsDTO']['ethernetInterfaces'][ 'ethernetInterface'] dev_found_interfaces = [] for dev_int in dev_interfaces: if 'description' in dev_int: #currently not working with comma seperated values. Check if each value in #similar to device/ids_by_desc split and remove brackets. abstract function? desc_list = args.description.split(",") # if values_dict['description'] in dev_int['description']: if all(x.casefold() in dev_int['description'].casefold() for x in desc_list): dev_found_interfaces.append(dev_int) ###### logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info( "---- found {} description match on switch ----".format( len(dev_found_interfaces))) for dev_int in dev_found_interfaces: logger.info("---- matching description #{} ----".format( dev_found_interfaces.index(dev_int) + 1)) self.desc_printer(dev_int, "interface :", 'name', logger) self.desc_printer(dev_int, "description :", 'description', logger) self.desc_printer(dev_int, "vlan :", 'accessVlan', logger) self.desc_printer(dev_int, "mac address :", 'macAddress', logger) self.desc_printer(dev_int, "status :", 'operationalStatus', logger) self.desc_printer(dev_int, "port mode :", 'desiredVlanMode', logger) self.desc_printer(dev_int, "allowed vlans :", 'allowedVlanIds', logger) self.desc_printer(dev_int, "speed :", 'speed', logger) self.desc_printer(dev_int, "duplex :", 'duplexMode', logger) return dev_found_interfaces def desc_active(self, args, config, logger): api_call = Client(config, logger) dev_id_list = api_call.ids_by_desc(args.description.strip()) logger.info("Occurrences of \"{}\" found: {} ".format( args.description, len(dev_id_list))) # exit out of loop if no matches if len(dev_id_list) < 1: sys.exit(1) for curr_id in dev_id_list: result = api_call.json_detailed( curr_id) # Modify this to go through multiple logger.info("------- Occurrence #{}--------\n".format( dev_id_list.index(curr_id) + 1)) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceName' ] neigh_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'deviceIpAddress', 'address' ] tmp = self.parse_json.value(result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'clientInterface' ] interface = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'ifDescr' ] description = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlan' ] vlan = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'vlanName' ] vlan_name = self.parse_json.value(result, key_list, logger) key_list = [ 'queryResponse', 'entity', 0, 'clientDetailsDTO', 'macAddress' ] mac_addr = self.parse_json.value(result, key_list, logger) logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) logger.info("interface :{}".format(interface)) logger.info("description :{}".format(description)) logger.info("vlan :{};{}".format(vlan, vlan_name)) logger.info("mac addr :{}".format(mac_addr)) return neigh_name, neigh_ip, interface, description, vlan, vlan_name, mac_addr def core(self, args, config, logger): # 400 critical error is thrown if description is not found api_call = Switch(config, logger) # check address to see if hostname or IP if "-" in args.address: dev_id = api_call.id_by_hostname(args.address.strip()) else: dev_id = api_call.id_by_ip(args.address.strip()) dev_result = api_call.json_basic(dev_id) result = api_call.json_detailed(dev_id) #create list of vlan interfaces (L3 & vlan database) dev_ip_interfaces = result['queryResponse']['entity'][0][ 'inventoryDetailsDTO']['ipInterfaces']['ipInterface'] dev_vlan_interfaces = result['queryResponse']['entity'][0][ 'inventoryDetailsDTO']['vlanInterfaces']['vlanInterface'] dev_modules = result['queryResponse']['entity'][0][ 'inventoryDetailsDTO']['modules']['module'] key_list = ['queryResponse', 'entity', 0, 'devicesDTO', 'deviceName'] neigh_name = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'devicesDTO', 'ipAddress'] tmp = self.parse_json.value(dev_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible #####if looking for a port add logic here( search for '/') if "/" in args.search_crit: dev_interfaces = result['queryResponse']['entity'][0][ 'inventoryDetailsDTO']['ethernetInterfaces'][ 'ethernetInterface'] dev_found_interfaces = [] for dev_int in dev_interfaces: #if values_dict['search_crit'] in dev_int['name']: if dev_int['name'].endswith(args.search_crit): dev_found_interfaces.append(dev_int) ###### logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) #dev_module is not working with BA switches #dev_module = self.list_parse_ends(values_dict['search_crit'], 'description', dev_modules, logger) #self.desc_printer(dev_module, "Transciever info :", 'description', logger) #logger.info("---- found {} description match on switch ----".format(len(dev_found_interfaces))) for dev_int in dev_found_interfaces: self.desc_printer(dev_int, "Admin Status :", 'adminStatus', logger) self.desc_printer(dev_int, "Operational Status :", 'operationalStatus', logger) self.desc_printer(dev_int, "Port :", 'name', logger) self.desc_printer(dev_int, "speed :", 'speed', logger) if 'description' in dev_int: self.desc_printer(dev_int, "description :", 'description', logger) if 'desiredVlanMode' in dev_int: if dev_int['desiredVlanMode'] == "ACCESS": self.desc_printer(dev_int, "Switchport Mode :", 'desiredVlanMode', logger) self.desc_printer(dev_int, "Access Vlan :", 'accessVlan', logger) self.vlan(str(dev_int['accessVlan']), dev_vlan_interfaces, dev_ip_interfaces, logger) elif dev_int['desiredVlanMode'] == "TRUNK": self.desc_printer(dev_int, "Switchport Mode :", 'desiredVlanMode', logger) self.desc_printer(dev_int, "Allowed Vlans :", 'allowedVlanIds', logger) elif 'desiredVlanMode' not in dev_int and dev_int[ 'operationalStatus'] == 'UP': logger.info( "This is a routed interface -- future functionality") #dev_vlan = self.vlan_parse(values_dict['search_crit'], dev_vlan_interfaces, dev_ip_interfaces,logger) else: #dev_vlan = self.vlan_parse(values_dict['search_crit'],dev_vlan_interfaces,dev_ip_interfaces,logger) self.vlan(args.search_crit, dev_vlan_interfaces, dev_ip_interfaces, logger) # return result def int(self, args, config, interface, logger): # 400 critical error is thrown if description is not found api_call = Switch(config, logger) # check address to see if hostname or IP if "-" in args.address: dev_id = api_call.id_by_hostname(args.address.strip()) else: dev_id = api_call.id_by_ip(args.address.strip()) dev_result = api_call.json_basic(dev_id) result = api_call.json_detailed(dev_id) key_list = ['queryResponse', 'entity', 0, 'devicesDTO', 'deviceName'] neigh_name = self.parse_json.value(dev_result, key_list, logger) key_list = ['queryResponse', 'entity', 0, 'devicesDTO', 'ipAddress'] tmp = self.parse_json.value(dev_result, key_list, logger) neigh_ip = socket.gethostbyname( tmp) # resolve fqdn to IP. Prime resolves IP if possible dev_interfaces = result['queryResponse']['entity'][0][ 'inventoryDetailsDTO']['ethernetInterfaces']['ethernetInterface'] found_match = [] for dev_int in dev_interfaces: if dev_int['name'].endswith(interface): found_match = dev_int ###### logger.info("switch name :{}".format(neigh_name)) logger.info("switch ip :{}".format(neigh_ip)) self.desc_printer(found_match, "interface :", 'name', logger) self.desc_printer(found_match, "description :", 'description', logger) self.desc_printer(found_match, "vlan :", 'accessVlan', logger) self.desc_printer(found_match, "voice vlan :", 'voiceVlan', logger) self.desc_printer(found_match, "mac address :", 'macAddress', logger) self.desc_printer(found_match, "status :", 'operationalStatus', logger) self.desc_printer(found_match, "port mode :", 'desiredVlanMode', logger) self.desc_printer(found_match, "allowed vlans :", 'allowedVlanIds', logger) self.desc_printer(found_match, "speed :", 'speed', logger) self.desc_printer(found_match, "duplex :", 'duplexMode', logger) return dev_id, found_match, neigh_ip def vlan(self, vlan_id, dev_vlan_interfaces, dev_ip_interfaces, logger): dev_vlan = self.list_parse_exact(int(vlan_id), 'vlanId', dev_vlan_interfaces, logger) dev_vlan = self.list_parse_exact(("Vlan" + vlan_id), 'name', dev_ip_interfaces, logger) ####Create a function to run through a list of dict [title:name]. then iterate through the #### list to print out the required things, so the 'if name in dev_int' is not required each time? self.desc_printer(dev_vlan, "Vlan ID :", 'name', logger) self.desc_printer(dev_vlan, "Admin Status :", 'adminStatus', logger) self.desc_printer(dev_vlan, "Operational Status :", 'operationalStatus', logger) self.desc_printer(dev_vlan, "ip address :", 'ipAddress', logger) return def list_parse_exact(self, find_match, find_str, dev_result, logger): match = False for dev_item in dev_result: if dev_item[find_str] == find_match: found_match = dev_item match = True if match == False: found_match = {1: 'nomatch'} return found_match def list_parse_ends(self, find_match, find_str, dev_result, logger): for dev_item in dev_result: if dev_item[find_str].endswith(find_match): found_match = dev_item return found_match def vlan_parse(self, vlan_id, dev_vlan_interfaces, dev_ip_interfaces, logger): #iterate through vlan interfaces (relevant if no L3 IP) if "/" not in vlan_id: # if this is not an interface (routed link) -- Not functional yet as not all routed interfaces show up for dev_vlan in dev_vlan_interfaces: if dev_vlan['vlanId'] == int(vlan_id): found_vlan = dev_vlan # iterate through ip interfaces (will overwrite found_vlan if L3 IP) for dev_vlan in dev_ip_interfaces: if dev_vlan['name'].endswith(vlan_id): found_vlan = dev_vlan return found_vlan def desc_printer(self, dev_int, log_str, key_val, logger): if key_val in dev_int: logger.info("{}{}".format(log_str, dev_int[key_val])) else: logger.info("{} N/A".format(log_str)) return