重命名接入点(用例)
阅读并遵循此示例,使用 Python 脚本和 API 快速重命名您的接入点 (AP)。
有时,您和其他无线管理员需要重命名特定站点Juniper Mist网络接入点 (AP)。一致的命名有助于提供网络清单文档的顺序和一致性。例如,站点可能会在物理上移动到新位置并采用新的命名约定。
若要执行此重命名任务,可以在Juniper Mist门户中使用正则表达式执行批量重命名作。此选项简单且随时可用,但如果要重命名许多设备,则可能会变得复杂。
另一种选择是创建 Python 脚本来重命名 AP。此选项简化了重命名许多设备的任务。下面的示例脚本演示了 Python 脚本重命名顺序。该脚本重命名已声明给站点的特定接入点。该脚本使用三个文件,如下所示:
-
第一个文件 (config.json) 包含配置变量。
-
第二个文件 (ap-names.csv) 是一个 CSV 文件,其中包含要重命名的接入点的MAC 地址以及接入点的新名称。
-
第三个文件 (main-rename-ap.py) 是 Python 脚本本身,它从其他两个文件中获取信息。
若要使用该脚本,请将所有三个文件放在同一个工作目录中。
第一个文件包含 JavaScript 对象标记 (JSON) 格式的配置,其中包括连接到 API 以进行所需调用以查找和重命名指定 AP 所需的变量。
-
使用 API 进行任何配置更改时,请确保您了解要修改的数据。此外,请务必执行验证,以确保一切仍然正常工作。
-
将占位符值替换为实际值,例如您的组织 ID、站点 ID、接入点名称等。
{
"api": {
"org_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"token": "ApUYc...hsO",
"mist_url": "https://{api-endpoint}.mist.com/api/v1/"
},
"site": {
"id": "{site-id}"
}
}
要代替 {api-endpoint},请使用全局区域的 API 终端节点。 请参阅 API 终端节点和全局区域。
Python 脚本使用 CSV 文件的内容来标识 AP(通过MAC 地址),然后将其重命名为新 名称ap-names.csv。
name,mac
{ap-name1},aabbcc001122
{ap-name2},aabbcc001123
{ap-name3},aabbcc001124
默认情况下,当您最初声明某个接入点时,该接入点将采用自己的 MAC 地址名称。主 Python 脚本执行查找和重命名 AP 的实际工作,其中包括以下函数:
-
(def) — 获取要重命名的接入点的 MAC 地址。
-
在网站上查找接入点。
-
重命名接入点。
main 函数 (def) 根据需要使用其他函数来完成任务和循环。
下面是 main-rename-ap.py 脚本的样子:
#!/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):
"""
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))
若要运行脚本,请调用该 main-rename-ap.py 脚本并提供config.json文件名和ap-names.csv文件名作为参数。例如:
user@linux-host} python main-rename-ap.py config.json ap-names.csv
运行脚本后,输出应如下所示:
** 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
您还可以查看Juniper Mist门户,并通过检查设备的库存来验证更改。自动化不仅限于 RESTful API 和 Python。您可以找到其他自动化选项,例如 WebSocket 和 Webhook API 用法以及帮助完成开发过程的工具。