Renaming Access Points (Use Case)
At times, you and other wireless administrators need to rename Juniper Mist network access points (APs) at a specific site. Consistent naming helps provide order and consistency in network inventory documentation. For example, a site may physically move to a new location and take on a new naming convention.
To perform this renaming task, in the Juniper Mist portal you can perform a bulk renaming action using a regular expression. This option is easy and readily available, but it can become complex if you have many devices to rename.
Another option is to create a Python script to rename the APs. This option simplifies the task of renaming many devices. The example script below illustrates the Python script renaming sequence. The script renames specific APs that have already been claimed to a site. The script uses three files, as follows:
-
The first file (config.json) contains the configuration variables.
-
The second file (ap-names.csv) is a CSV file that contains the MAC address of the AP to be renamed as well as the AP’s new name.
-
The third file (main-rename-ap.py) is the Python script itself, which takes information from the other two files.
To use the script, you place all three files in the same working directory.
The first file, which contains the JavaScript Object Notation (JSON) formatted configuration, includes variables needed to connect to the API to make the required calls to find and rename the specified APs.
When making any configuration changes using the API, make sure you understand the data that you are modifying. Also be sure to perform validation to ensure that things are still working properly.
{ "api": { "org_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "token": "ApUYc...hsO", "mist_url": "https://{api-endpoint}.mist.com/api/v1/" }, "site": { "id": "{site-id}" } }
In place of {api-endpoint}, use the API endpoint for your global region. See API Endpoints and Global Regions.
The Python script uses the contents of the CSV file to identify the AP (by the MAC address) and then rename it to the new name ap-names.csv.
name,mac {ap-name1},aabbcc001122 {ap-name2},aabbcc001123 {ap-name3},aabbcc001124
By default, when you initially claim an AP, the AP takes the name of its own MAC address. The main Python script does the actual work of finding and renaming of the AP, which consists of the following functions:
-
(def)—Get the MAC address of the AP to rename.
-
Find the AP on the site.
-
Rename the AP.
The main function (def) uses the other functions to complete the task and loops, as necessary.
This is what the main-rename-ap.py script looks like:
#!/usr/bin/env python3 """ The format of the MAC address part of this CSV file must be the following: aabbccddeeff """ import argparse import time import json import requests import csv from pprint import pprint def is_ap_in_site(configs: dict, ap_mac: str): """ This function checks for an AP assigned to a site. Parameters: - configs: dictionary containing all configuration information - site_id: ID of the site we will to assign the AP to Returns: - the ID of the AP if the AP is assigned to the site - the current name of the AP """ api_url = f"{configs['api']['mist_url']}sites/{configs['site']['id']}/devices" headers = {'Content-Type': 'application/json', 'Authorization': 'Token {}'.format(configs['api']['token'])} response = requests.get(api_url, headers=headers) if response.status_code == 200: devices = json.loads(response.content.decode('utf-8')) for device in devices: if device['mac'] == ap_mac: return (device['id'], device['name']) else: print('Something went wrong: {}'.format(response.status_code)) return (None, None) def rename_ap(configs: dict, ap_id: str, new_ap_name: str, ap_old_name: str): """ This function renames an AP. Parameters: - configs: dictionary containing all configuration information - ap_id: ID of the AP device object - new_ap_name: name to apply to the AP - ap_old_name: current name of the AP """ api_url = f"{configs['api']['mist_url']}sites/{configs['site']['id']}/devices/{ap_id}" headers = {'Content-Type': 'application/json', 'Authorization': 'Token {}'.format(configs['api']['token'])} body = {} body['name'] = new_ap_name response = requests.put(api_url, headers=headers, data=json.dumps(body)) if response.status_code == 200: device = json.loads(response.content.decode('utf-8')) print(f"{device['mac']} renamed from {ap_old_name} to {device['name']}") else: print(f"AP ID: {ap_id}\tSomething went wrong: {response.status_code}") def retreive_ap_mac_list(csv_filename: str) -} dict: """ This function converts the content of the CSV file to a Python dictionary. Parameters: - csv_filename: the name of the comma separated value file. Returns: - A dictionary containing the content of the CSV file """ ap_csv = csv.DictReader(csv_filename) ap_list = [] for line in ap_csv: ap_list.append(line) return ap_list def main(): """ This script batch renames the APs listed in a CSV file. """ parser = argparse.ArgumentParser(description='Configures a Mist AP for an APoS site survey') parser.add_argument('config', metavar='config_file', type=argparse.FileType( 'r'), help='file containing all the configuration information') parser.add_argument('ap_list', metavar='aps_names', type=argparse.FileType( 'r'), help='csv file containing new AP names') args = parser.parse_args() configs = json.load(args.config) ap_mac_list = retreive_ap_mac_list(args.ap_list) for ap in ap_mac_list: ap_id, ap_old_name = is_ap_in_site(configs, ap['mac']) if ap_id: rename_ap(configs, ap_id, ap['name'], ap_old_name) else: print(f"AP {ap['name']} is not part of site {configs['site']['id']}") if __name__ == '__main__': start_time = time.time() print('** Start the batch renaming of APs...\n') main() run_time = time.time() - start_time print("\n** Time to run: %s sec" % round(run_time, 2))
To run the script, call the main-rename-ap.py
script and provide the
config.json filename and the ap-names.csv filename as arguments. For example:
user@linux-host} python main-rename-ap.py config.json ap-names.csv
After you run the script, the output should look something like this:
** Start the batch renaming of APs... aabbcc001121 renamed from OLD-AP-01 to NEW-AP-01 aabbcc001122 renamed from OLD-AP-02 to NEW-AP-02 aabbcc001122 renamed from OLD-AP-03 to NEW-AP-03 ** Time to run: 3.24 sec
You can also check the Juniper Mist portal and verify the changes by checking the device’s inventory. Automation is not limited to RESTful APIs and Python. You can find other automation options such as WebSocket and webhook API usage and tools to help in the development process.