アクセスポイントの名前変更(ユースケース)
場合によっては、あなたや他のワイヤレス管理者は、特定のサイトのJuniper Mistネットワークアクセスポイント(AP)の名前を変更する必要があります。一貫性のある命名は、ネットワーク インベントリ ドキュメントの順序と一貫性を提供するのに役立ちます。たとえば、サイトが物理的に新しい場所に移動して、新しい名前付け規則を採用する場合があります。
この名前変更タスクを実行するには、Juniper Mist ポータルで正規表現を使用して一括名前変更アクションを実行します。このオプションは簡単ですぐに使用できますが、名前を変更するデバイスが多数ある場合は複雑になる可能性があります。
別のオプションは、APの名前を変更するPythonスクリプトを作成することです。このオプションにより、多くのデバイスの名前を変更する作業が簡略化されます。以下のサンプル スクリプトは、Python スクリプトの名前変更シーケンスを示しています。このスクリプトは、すでにサイトに要求されている特定のAPの名前を変更します。このスクリプトでは、次の 3 つのファイルを使用します。
-
最初のファイル(config.json)には、構成変数が含まれています。
-
2 番目のファイル(ap-names.csv)は、名前を変更する AP の MAC アドレスと AP の新しい名前を含むCSVファイルです。
-
3 番目のファイル (main-rename-ap.py) は Python スクリプト自体で、他の 2 つのファイルから情報を取得します。
スクリプトを使用するには、3 つのファイルすべてを同じ作業ディレクトリに配置します。
最初のファイルには、JavaScript Object Notation(JSON)形式の設定が含まれており、指定されたAPを検索して名前を変更するために必要な呼び出しを行うためにAPIに接続するために必要な変数が含まれています。
API を使用して構成を変更する場合は、変更するデータを理解してください。また、正常に動作していることを確認するために、必ず検証を実行してください。
{ "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ファイルの内容を使用して(MACアドレスで)APを識別し、名前を 新しい名前ap-names.csvに変更します。
name,mac {ap-name1},aabbcc001122 {ap-name2},aabbcc001123 {ap-name3},aabbcc001124
デフォルトでは、最初に AP をクレームすると、AP は自身の MAC アドレスの名前を取得します。メインのPythonスクリプトは、次の機能で構成されるAPの検索と名前変更の実際の作業を行います。
-
(def)—名前を変更する AP の MAC アドレスを取得します。
-
サイトでAPを見つけます。
-
APの名前を変更します。
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) -} 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))
スクリプトを実行するには、 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 の使用法など、開発プロセスに役立つ他の自動化オプションやツールを見つけることができます。