def main(): timer = StopWatch() timer.start() if not api_token: print( "You must define a valid API key using the MIST_TOKEN environmental variable name to use this script...exiting." ) sys.exit() if not site_id: print( "You must define a valid site ID using the MIST_SITE_ID environmental variable name to use this script...exiting." ) sys.exit() header() logger.info("Getting clients.") verb_obj = MistVerbs(api_token) clients = verb_obj.mist_read(clients_url) pprint(clients) logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() if not api_token: print("You must define a valid API key using the MIST_TOKEN environmental variable name to use this script...exiting.") sys.exit() if not token_id: print("You must pass a valid token ID via the CLI ti use this script...exiting.") sys.exit() header() # List tokens logger.info("Deleting supplied token ID.") verb_obj = MistVerbs(api_token, False) verb_obj.mist_delete("{}/{}".format(tokens_url, token_id)) logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() mist = MistiFi(token=os.getenv('MIST_TOKEN')) mist.comms() # The library takes care of the below #if not api_token: # print("You must define a valid API key using the MIST_TOKEN environmental variable name to use this script...exiting.") # sys.exit() header() logger.info("Getting tokens.") tokens = mist.apitokens() #verb_obj = MistVerbs(api_token) #tokens = verb_obj.mist_read(tokens_url) pprint(tokens) logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() if not api_token: print( "You must define a valid API key using the MIST_TOKEN environmental variable name to use this script...exiting." ) sys.exit() # Create tokens verb_obj = MistVerbs(api_token, False) token = verb_obj.mist_create(tokens_url) header() print( "Please record the API token key shown below, this is only avaiable \n at the time of creation and cannot be retrieved later:" ) print("\n Token key: {}".format(token['key'])) print("\n (Token ID, which is not required for API access: {} \n".format( token['id'])) logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() if not api_token: print("You must define a valid API token using the MIST_TOKEN environment variable name to use this script...exiting.") sys.exit() if not org_id: print("You must define a valid organization ID using the MIST_ORG environment variable name to use this script...exiting.") sys.exit() header() # Get my org sites logger.info("Getting org info.") verb_obj = MistVerbs(api_token) org_info = verb_obj.mist_read(org_stats_url) """ Data structure returned: { "num_inventory": 12, "num_devices": 10, "name": "Sam MSP Customer 5", "orggroup_ids": [], "allow_mist": false, "num_devices_connected": 8, "num_devices_disconnected": 2, "num_sites": 1, "num_clients": 80, "id": "448a3934-df89-11e5-8898-1258369c38a9", "sle": [ {"path": "coverage", "user_minutes": {"total": 20, "ok": 19}}, {"path": "capacity", "user_minutes": {"total": 20, "ok": 18}}, {"path": "time-to-connect", "user_minutes": {"total": 20, "ok": 19}} ] } """ print(f""" Org name = {org_info['name']} Claimed Devices: {org_info['num_inventory']} Devices in use: {org_info['num_devices']} Connected Devices: {org_info['num_devices_connected']} Disconnected Devices: {org_info['num_devices_disconnected']} Number Sites: {org_info['num_sites']} """) logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() if not api_token: print( "You must define a valid API token using the MIST_TOKEN environmental variable name to use this script...exiting." ) sys.exit() header() # Get my org sites logger.info("Getting sites info.") verb_obj = MistVerbs(api_token) devices_on_site = verb_obj.mist_read(site_inventory_url) dict_data = [] for device in devices_on_site: dict = { "device_name": device['name'], "device_model": device['model'], "device_type": device['type'], "device_serial": device['serial'], } if device['type'] == 'ap': dict_data.append(dict) logger.info("Dumping CSV device report file: {}.".format(report_file)) column_headers = [ "device_name", "device_model", "device_type", "device_serial" ] try: with open(report_file, 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=column_headers) writer.writeheader() for dict in dict_data: writer.writerow(dict) except IOError as err: logger.error("CSV I/O error: {}".format(err)) logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() if not api_token: print( "You must define a valid API key using the MIST_TOKEN environmental variable name to use this script...exiting." ) sys.exit() header() # List tokens logger.info("Getting tokens.") verb_obj = MistVerbs(api_token, False) tokens = verb_obj.mist_read(tokens_url) pprint(tokens) # Verify ID of token we are using api_token_short = "{}...{}".format(api_token[0:4], api_token[-4:]) our_token_id = None for token in tokens: if token['key'] == api_token_short: our_token_id = token['id'] # Delete all tokens apart from our current token if len(tokens) > 1: logger.info("Deleting tokens.") for token in tokens: if token['id'] != our_token_id: logger.info("Deleting tokens ID: {}".format(token['id'])) verb_obj.mist_delete("{}/{}".format(tokens_url, token['id'])) else: logger.info("No tokens to tidy up.") logger.info("Script complete.") timer.stop() footer()
def main(): timer = StopWatch() timer.start() if not api_token: print( "You must define a valid API key using the MIST_TOKEN environmental variable name to use this script...exiting." ) sys.exit() if not site_id: print( "You must define a valid site ID using the MIST_SITE_ID environmental variable name to use this script...exiting." ) sys.exit() header() logger.info("Getting tokens.") verb_obj = MistVerbs(api_token) apple_devices = verb_obj.mist_read(apple_clients_url) dict_data = [] mac_list = [] for device in apple_devices['results']: dict = { "client_manufacture": device['client_manufacture'], "client_family": device['client_family'], "client_model": device['client_model'], "client_os": device['client_os'], "band": device['band'], "mac": device['mac'], } # add to device dict, but do not add duplicates if not device['mac'] in mac_list: dict_data.append(dict) mac_list.append(device['mac']) logger.info("Dumping CSV device report file: {}.".format(report_file)) column_headers = [ "client_manufacture", "client_family", "client_model", "client_os", "mac", "band" ] try: with open(report_file, 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=column_headers) writer.writeheader() for dict in dict_data: writer.writerow(dict) except IOError as err: logger.error("CSV I/O error: {}".format(err)) logger.info("Script complete.") timer.stop() footer()
def check_env(): total_tests = 4 passed_count = 0 header() print( "Executing tests to check if our environment is \nsuitable to use Mist API:\n" ) print("1. Checking our DNS is good (looking up api.mist.com)...") try: socket.gethostbyname("api.mist.com") print(" Result: OK.\n") passed_count += 1 except: print( " Result: ** Fail ** (Check your DNS settings or network connectivity) .\n" ) base_url = "https://api.mist.com" print("2. Checking we can get to Mist API URL ({})...".format(base_url)) session = requests.Session() try: session.get(base_url, timeout=1) print(" Result: OK.\n") passed_count += 1 except: print(" Result: ** Fail ** (Check network path available to {})\n". format(base_url)) print("3. Checking we have an API key defined (via env var MIST_TOKEN)...") if api_token: print(" Result: OK.\n") passed_count += 1 else: print( " Result: ** Fail **. (Please define the env var MIST_TOKEN)\n") print("4. Try a 'who am I' via the API...") headers = { 'Content-Type': 'application/json', 'Authorization': 'Token {}'.format(api_token) } url = "https://api.mist.com//api/v1/self" try: response = session.get(url, headers=headers, timeout=2) if response.status_code == 200: print(" Result: OK. (result below)\n") data = json.loads(response.content.decode('utf-8')) print(" Name: {} {}".format(data['first_name'], data['last_name'])) print(" Email: {}".format(data['email'])) passed_count += 1 else: status_code = response.status_code print( " Result: ** Fail. ** (reponse received, but expected data was not received (code: {}, text: {}))" .format(status_code, responses[status_code])) print( " (Maybe check that API token is available (via env var MIST_TOKEN and is valid)" ) except: print(" Result: ** Fail **. (Please check network connectivity)\n") print("\n -- Tests complete (passed {}/{}) --".format( passed_count, total_tests)) footer()