class Poke: def __init__(self): self.find = Find() def port(self, args, config, logger): # Currently working with IP. Change this to be similar to find core where it parses dev_id, found_int, dev_ip = self.find.int(args, config, args.interface, logger) return args def vlan(self, args, config, logger): # Currently working with IP. Change this to be similar to find core where it parses? dev_id, found_int, dev_ip = self.find.int(args, config, args.interface, logger) return args
class Change: def __init__(self): self.find = Find() def mac_vlan(self, values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger): # find and display neigh_name, neigh_ip, interface, description, old_vlan, old_vlan_name, addr = \ self.find.mac_client(values_dict, cpi_username, cpi_password, cpi_ipv4_address, logger) # require 'yes' input to proceed logger.info('Change VLAN to: {}'.format(values_dict['vlan'])) response = input("Confirm action of changing VLAN ('yes'):") if not response == 'yes': logger.info('Did not proceed with change.') sys.exit(1) # invoke API call to change VLAN sw_api_call = Switch(cpi_username, cpi_password, cpi_ipv4_address, logger) dev_id = sw_api_call.id_by_ip(neigh_ip) job_id = sw_api_call.conf_if_vlan(dev_id, interface, "Access", values_dict['vlan']) timeout = time.time() + 30 # 30 second timeout starting now while not sw_api_call.job_complete(job_id): time.sleep(5) if time.time() > timeout: logger.critical("Change VLAN failed. Prime job not completed") sys.exit(1) if not sw_api_call.job_successful(job_id): logger.critical("Change VLAN failed. Prime job not successful") sys.exit(1) logger.info('Change VLAN complete.')
def __init__(self): self.find = Find() self.parse_json = JsonParser()
def __init__(self): craft = CliCrafter() # ----- base sub-commands find_sp = craft.find_subparser(craft.subparsers) mock_sp = craft.mock_subparser(craft.subparsers) change_sp = craft.change_subparser(craft.subparsers) # ----- capt find ip x.x.x.x find_ip = craft.ip_parser(find_sp) craft.addr_arg(find_ip) find_ip.set_defaults(func=CliParser.find_ip) # ----- capt find ip x.x.x.x --ap craft.ap_arg(find_ip) # ----- capt find ip x.x.x.x --phone craft.phone_arg(find_ip) # ----- capt find mac xx:xx:xx:xx:xx:xx find_mac = craft.mac_parser(find_sp) craft.addr_arg(find_mac) find_mac.set_defaults(func=CliParser.find_mac) # ----- capt find mac xx:xx:xx:xx:xx:xx --ap craft.ap_arg(find_mac) # ----- capt find mac xx:xx:xx:xx:xx:xx --phone craft.phone_arg(find_mac) # ----- capt upgrade x.x.x.x upgrade = craft.upgrade_parser(craft.subparsers) craft.addr_arg(upgrade) upgrade.set_defaults(func=CliParser.upgrade) # ----- capt mock upgrade x.x.x.x mock_upgrade = craft.upgrade_parser(mock_sp) craft.addr_arg(mock_upgrade) mock_upgrade.set_defaults(func=CliParser.mock_upgrade) # ----- capt change mac xx:xx:xx:xx:xx:xx --vlan yyyy change_mac = craft.mac_parser(change_sp) craft.addr_arg(change_mac) craft.vlan_arg(change_mac) change_mac.set_defaults(func=CliParser.change_mac) # ----- capt test_api test_api_sp = craft.test_api_subparser(craft.subparsers) test_api_mac = craft.mac_parser(test_api_sp) craft.addr_arg(test_api_mac) test_api_mac.set_defaults(func=CliParser.test_api_mac) argcomplete.autocomplete(craft.parser) args = craft.parser.parse_args() cli_parse = CliParser(args) # if sub commands if cli_parse.sub_cmd_exists(): command, values_dict = args.func( cli_parse) # execute argument_parser function config.load_base_conf() if cli_parse.first_sub_cmd() == 'find' or cli_parse.first_sub_cmd( ) == 'change': log_file = False elif cli_parse.first_sub_cmd( ) == 'upgrade' or cli_parse.first_sub_cmd() == 'mock': log_file = True else: log_file = True logger = self.set_logger(args.address, logging.INFO, log_file) find = Find() change = Change() if command == 'find_ip': find.ip_client(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'find_ip--ap': find.ip_ap(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'find_ip--phone': find.ip_phone(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'find_mac': find.mac_client(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'find_mac--ap': find.mac_ap(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'find_mac--phone': find.mac_phone(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'change_mac--vlan': change.mac_vlan(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'upgrade': UpgradeCode(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'mock_upgrade': MockUpgradeCode(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) if command == 'test_api_mac': TestApi.test_method(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) else: # no sub commands config.load_full_conf() self.main(args.debug)
def __init__(self): self.find = Find()
def __init__(self): # create CLICrafter object to parse CLI input craft = CliCrafter() #argcomplete.autocomplete(craft.parser) # create a namespace object of the arguments. If using a -h, the code will exit here # after this point the program has all of the chosen command line arguments args = craft.parser.parse_args() #arg_dict = vars(args);# this could turn the args into a dictionary to pass, is it necessary though? cli_parse = CliParser(args) if args.sub_cmd is not None: config.load_base_conf() # should config use globals? # subber = cli_parse.first_sub_cmd() # if subber == 'find' or subber == 'change' or subber == 'tools' or subber == 'poke' or subber == 'push': # log_file = False # elif subber == 'upgrade' or subber == 'mock': # log_file = True # else: # log_file = True #Revisit logging code log_file = True try: logger = self.set_logger(args.address, logging.INFO, log_file) except AttributeError: #address does not exist try: logger = self.set_logger(args.description, logging.INFO, log_file) except AttributeError: try: logger = self.set_logger(args.tools, logging.INFO, log_file) except AttributeError: #do something here try: logger = self.set_logger(args.reports, logging.INFO, log_file) except AttributeError: try: logger = self.set_logger( args.push, logging.INFO, log_file) except AttributeError: # sys_logger.critical("Address and description not found.") sys.exit(1) # if'email' in args and args.email is not None: # self.set_logger_email(config, args.email,logger) if cli_parse.args.sub_cmd == 'find': find = Find() if cli_parse.args.find == 'ip': if not (cli_parse.args.ap or cli_parse.args.phone): find.ip_client(args, config, logger) elif cli_parse.args.ap and not cli_parse.args.phone: find.ip_ap(args, config, logger) elif cli_parse.args.phone and not cli_parse.args.ap: find.ip_phone(args, config, logger) elif cli_parse.args.find == 'mac': args.address = CliParser.normalize_mac(args.address) if not (cli_parse.args.ap or cli_parse.args.phone): find.mac_client(args, config, logger) #find.old_mac_client(values_dict, config.username, config.password, config.cpi_ipv4_address, logger) elif cli_parse.args.ap and not cli_parse.args.phone: find.mac_ap(args, config, logger) elif cli_parse.args.phone and not cli_parse.args.ap: find.mac_phone(args, config, logger) elif cli_parse.args.find == 'desc': if not args.active: find.desc(args, config, logger) else: find.desc_active(args, config, logger) elif cli_parse.args.find == 'core': find.core(args, config, logger) elif cli_parse.args.sub_cmd == 'change': change = Change() change.mac_vlan(args, config, logger) elif cli_parse.args.sub_cmd == 'push': push = Push() if cli_parse.args.push == 'bas': push.bas(args, config, logger) elif cli_parse.args.push == 'template': push.template(args, config, logger) elif cli_parse.args.sub_cmd == 'poke': poke = Poke() if cli_parse.args.poke == 'port': poke.port(args, config, logger) elif cli_parse.args.sub_cmd == 'reports': reports = Reports() if cli_parse.args.reports == 'portcount': reports.port_count(args, config, logger) elif cli_parse.args.reports == 'devcount': reports.dev_count(args, config, logger) elif cli_parse.args.reports == 'vlanmap': reports.vlanmap(args, config, logger) elif cli_parse.args.reports == 'servicematrix': reports.service_matrix(args, config, logger) elif cli_parse.args.sub_cmd == 'mock': if cli_parse.args.mock == 'upgrade': MockUpgradeCode(args, config, logger) elif cli_parse.args.sub_cmd == 'upgrade': UpgradeCode(args, config, logger) elif cli_parse.args.sub_cmd == 'tools': tools = Tools() if cli_parse.args.tools == 'apcheck': if cli_parse.args.apcheck == "alarms": tools.checkAlarms(args, config, logger) elif cli_parse.args.apcheck == "slow_ports": tools.slow_aps(args, config, logger) elif cli_parse.args.apcheck == "unack": tools.un_ack_alarms(args, config, logger) # if 'days' in self.args and self.args.days is not None: # dict_of_values = {'days': self.args.days} # else: # dict_of_values = {'days': "all"} elif cli_parse.args.sub_cmd == 'test_api': TestApi.test_method(args, config, logger) if 'email' in args and args.email is not None: if 'reports' in args and args.csv: with open(reports.filename + '.csv', 'r') as file: data = file.read() else: with open(config.logpath, 'r') as file: #data = file.read().replace('\n', '') data = file.read() try: smtpObj = smtplib.SMTP(config.email_host) smtpObj.sendmail(config.email_from, [args.email], data) #smtpObj.sendmail(config.email_from, [args.email], email_string ) logger.info("successfully sent Email") except smtplib.SMTPException: logger.info("Failed to send Email") except Exception as e: logger.info(e)
class Push: def __init__(self): self.find = Find() def template(self, args, config, logger): dev_id_list = [] address_list = [] try: file = open(os.path.join(args.file_name), "r") for ip in file: dev_id = self.find.dev_id(args, config, ip, logger) time.sleep(1) dev_id_list.append({"targetDeviceID": "{}".format(dev_id)}) address_list.append({"address": "{}".format(ip.strip())}) file.close() except FileNotFoundError: print("##### ERROR iplist files not found #####") except Exception as err: print("##### ERROR with processing:{} #####".format(err)) # require 'yes' input to proceed # logger.info('Activate BAS on switch INTERFACE {} using VLAN: {}'.format(found_int['name'], args.vlan)) # response = input("Confirm action of changing VLAN ('yes'):") # if not response == 'yes': # logger.info('Did not proceed with change.') # sys.exit(1) # invoke API call to change VLAN sw_api_call = Switch(config, logger) # create API switch call object # push API_CALL_conf_if_bas template out. Update this to use a shared template, the same as change vlan? job_id = sw_api_call.conf_template(dev_id_list, args.template_name) timeout = time.time() + 30 # 30 second timeout starting now time.sleep( 1 ) # without the sleep the job_complete can balk, not finding the job_id yet while not sw_api_call.job_complete(job_id): time.sleep(5) if time.time() > timeout: logger.critical( "Template push failed. Prime job not completed") sys.exit(1) ###################Only sync successful? self.force_sync_multiple(address_list, sw_api_call) # 20 minute timeout ################# if not sw_api_call.job_successful(job_id): logger.critical("Template push failed. Prime job not successful") sys.exit(1) logger.info("Synchronizing ...") # logger.info("Synchronized!") logger.info('Template push complete.') return args def bas(self, args, config, logger): # find and display (update this call to work) dev_id, found_int, dev_ip = self.find.int(args, config, args.interface, logger) # require 'yes' input to proceed logger.info( 'Activate BAS on switch INTERFACE {} using VLAN: {}'.format( found_int['name'], args.vlan)) response = input("Confirm action of changing VLAN ('yes'):") if not response == 'yes': logger.info('Did not proceed with change.') sys.exit(1) # invoke API call to change VLAN # sw_api_call = Switch(config.username, config.password, config.cpi_ipv4_address, logger) # create API switch call object sw_api_call = Switch(config, logger) # create API switch call object # push API_CALL_conf_if_bas template out. Update this to use a shared template, the same as change vlan? job_id = sw_api_call.conf_if_bas(dev_id, found_int['name'], args.description, args.vlan) timeout = time.time() + 30 # 30 second timeout starting now time.sleep( 1 ) # without the sleep the job_complete can balk, not finding the job_id yet while not sw_api_call.job_complete(job_id): time.sleep(5) if time.time() > timeout: logger.critical("Change VLAN failed. Prime job not completed") sys.exit(1) if not sw_api_call.job_successful(job_id): logger.critical("Change VLAN failed. Prime job not successful") sys.exit(1) logger.info('Change VLAN complete.') ######################################################## #add a verification flag to sync and display after, instead of default? ######################################################## logger.info("Synchronizing ...") self.force_sync(dev_id, dev_ip, sw_api_call, 20, logger) # 20 minute timeout logger.info("Synchronized!") dev_id, found_int, dev_ip = self.find.int(args, config, args.interface, logger) return args def force_sync_multiple(self, address_list, sw_api_call): #no error handling, for triggering a config backup sw_api_call.sync_multiple(address_list) # force a sync! # Copies of synchronized and force_sync from upgrade_code.py That uses a constant to hold values though def force_sync(self, sw_id, sw_ip, sw_api_call, timeout, logger): old_sync_time = sw_api_call.sync_time(sw_id) sw_api_call.sync(sw_ip) # force a sync! end_time = time.time() + 60 * timeout logger.info("Timeout set to {} minutes.".format(timeout)) time.sleep( 20) # don't test for sync status too soon (CPI delay and all that) while not self.synchronized(sw_id, sw_api_call, logger): time.sleep(10) if time.time() > end_time: logger.critical("Timed out. Sync failed.") sys.exit(1) new_sync_time = sw_api_call.sync_time(sw_id) if old_sync_time == new_sync_time: # KEEP CODE! needed for corner case where force sync fails (code 03.03.03) logger.critical( "Before and after sync time is the same. Sync failed.") sys.exit(1) # def force_sync_multiple(self, sw_id,sw_ip, sw_api_call, timeout, logger): # old_sync_time = sw_api_call.sync_time(sw_id) # sw_api_call.sync(sw_ip) # force a sync! # end_time = time.time() + 60 * timeout # logger.info("Timeout set to {} minutes.".format(timeout)) # time.sleep(20) # don't test for sync status too soon (CPI delay and all that) # while not self.synchronized(sw_id, sw_api_call, logger): # time.sleep(10) # if time.time() > end_time: # logger.critical("{} Timed out. Sync failed.".format(sw_ip)) # return # # new_sync_time = sw_api_call.sync_time(sw_id) # if old_sync_time == new_sync_time: # KEEP CODE! needed for corner case where force sync fails (code 03.03.03) # logger.critical("{} Before and after sync time is the same. Sync failed.".format(sw_ip)) # return def synchronized(self, sw_id, sw_api_call, logger): if sw_api_call.sync_status(sw_id) == "COMPLETED": logger.info("Synchronization Complete!") return True elif sw_api_call.sync_status(sw_id) == "SYNCHRONIZING": return False else: #sw.sync_state = sw_api_call.sync_status(sw_id) logger.warning("Unexpected sync state:") return False