LoginSignup
14
2

More than 5 years have passed since last update.

AIスピーカーでネットワーク管理してみました

Last updated at Posted at 2018-12-23

はじめに

AIスピーカーを利用してネットワークの稼動状況や設定変更がコマンド入力や管理画面に接続することなく誰でも行うことができないか試してみました。

今回使用した機器は、Amazon EchoとHPE Aruba Networks製品(無線AP)。
バックエンドにはAWS Lambdaを利用しクラウド上でネットワーク管理できるAruba Central経由で
ネットワークの設定変更してみました。
実際の対話はこのようになります。

スクリーンショット 2018-11-28 11.27.01.png

全体構成図

スクリーンショット 2018-11-28 13.37.25.png

①.Alexaに設定変更を依頼
②.Alexa Intentでユーザ発言を変数に変換してLambdaへリクエストを送信
③.LambdaはまずAruba Centralに接続するためのアクセストークンとリクエスト内容により設定テンプレートをS3から取得
④.S3より取得した設定テンプレート一時的にLambda内に保存
⑤.Lambda上のスクリプトがAruba CentralのAPIを使って接続し設定テンプレートを書き込み
⑥.Aruba Centralは設定されたテンプレートを各無線APに配信し変更を反映
⑦.Alexaが変更結果を返答


●準備するアカウント

  • Amazon開発アカウント
    • Alexaスキルを開発するために必要  
  • AWSアカウント
    • AWS Lambdaを開発するために必要
  • Aruba Centralアカウント
    • Aruba Centralへの接続やAPIを確認するために必要

作成方法

Alexaスキルと対話モデルの作り方

Amazon開発アカウントでAlexa Skills Kit Developer Consoleに接続してAlexaスキルと対話モデルを作成します。
Alexaスキルの基本的な作成方法は公式サイトを参照してください。
Alexaスキル開発トレーニング

今回のインテントの例とサンプル発話の例です。


"name": "GuestwifiON",
                    "slots": [],
                    "samples": [
                        "ゲストをオンして",
                        "ゲストワイファイを有効",
                        "ゲストワイハイを有効",
                        "ゲストをオン",
                        "ゲストを有効にして"

 "name": "GuestwifiOFF",
                    "slots": [],
                    "samples": [
                        "ゲストをオフして",
                        "ゲストを無効にして",
                        "ゲストを停止して",
                        "ゲストワイファイをオフ",
                        "ゲストワイファイをオフして",
                        "ゲストをオフ",
                        "ゲストをオフに設定"

AWS Lambda関数の作り方

AWS Lambdaの作成方法についてはこちらを参照してください。
AWS Lambda ドキュメント
今回は、ランタイムとしてPython 3.6を使用

アクセストークンについて

Aruba Centralに接続するためにはアクセストークンが必要となります。このアクセストークンは2時間毎に変更となる仕様となってますので今回は、定期的にアクセストークンを取得しS3に保存することにしAruba Centralに接続時にS3に保存されたトークンファイルを参照する仕組みにしました。
下記コードはLambdaでテンプレートを使用した作成した内容に書き加えた内容となります。


#アクセストークン取得#
#### API_Token ####
def refresh_token(token_dict):
    client_id = token_dict['client_id']
    client_secret = token_dict['client_secret']
    refresh_token = token_dict['refresh_token']
    url = base_url+"oauth2/token?client_id="+client_id+"&client_secret="+client_secret+"&refresh_token="+refresh_token+"&grant_type=refresh_token"
    print(url)
    response = requests.request("POST", url)
    print(response)
    token_dict['refresh_token'] = response.json()['refresh_token']
    token_dict['access_token'] = response.json()['access_token']
    access_token = response.json()['access_token']
    text = json.dumps(token_dict)
    upload_s3(text,"central_token.json")
    return access_token

#S3にアップロード#
#### Upload Json to S3 ####
def upload_s3(text,key):
    s3 = boto3.client('s3')
    handler = StringIO(text)
    s3.put_object(
        Bucket = bucket_name,
        Key = key,
        Body = handler.read(),
        ContentType = 'application/json; charset=utf-8'
    )
#S3からトークンを参照#
#### read Json to S3 ####
def read_s3():
    s3 = boto3.resource('s3')
    content_object = s3.Object(bucket_name, 'central_token.json')
    file_content = content_object.get()['Body'].read().decode('utf-8')
    json_content = json.loads(file_content)
    return json_content['access_token']

GuestWiFi変更について

事前に設定変更用のテンプレートをS3に準備し設定変更内容に応じてファイルを取得し
Aruba CentralのAPIを利用して設定変更を行うようにしました。すみません今回は設定変更したい部分だけでなくテンプレートを使用してコンフィグを全て変更してます。もっといい方法があると思いますが。。。


#設定変更用テンプレート取得#
#### Guestwifi Json to S3 ####
def download_s3(intent):
    s3 = boto3.resource('s3')
    if intent == "GuestwifiON":
        file_name = "enable.txt"
    elif intent =="GuestwifiOFF":
        file_name = "disable.txt"
    file_path = file_path = '/tmp/' + file_name
    bucket = s3.Bucket(bucket_name)   
    bucket.download_file(file_name, file_path)
    content_object = s3.Object(bucket_name, file_name)
    print(subprocess.run(["ls", "-l", "/tmp"], stdout=subprocess.PIPE))
    return

#GuestwifiをONする場合#
#### Guestwifi ON ####
    session_attributes = {}
    download_s3(intent["name"])
    access_token = read_s3()
    fileName = '/tmp/enable.txt'
    fileDataBinary = open(fileName, 'rb').read()
    files = {'template':(fileName,fileDataBinary,'text/plain')}
    url = "https://app1-apigw.central.arubanetworks.com/configuration/v1/groups/tec-temp/templates"
    querystring = {"name":"temp01","device_type":"IAP","access_token":access_token}

    response = requests.request("PATCH", url, params=querystring, files=files)
    print(response.text)


    if response.status_code == requests.codes.ok:
        speech_output = 'ゲストワイファイを有効にしました';
        reprompt_text ='もう一度お願いします';
        should_end_session = True

    else:
        speech_output = 'ゲストワイファイを有効にできませんでした。';
        reprompt_text ='もう一度お願いします';
        should_end_session = True


#GuestwifiをOFFする場合#
#### Guestwifi OFF ####
def GuestwifiOFF(intent, session):
    session_attributes = {}
    download_s3(intent["name"])
    access_token = read_s3()
    fileName = '/tmp/disable.txt'
    fileDataBinary = open(fileName, 'rb').read()
    files = {'template':(fileName,fileDataBinary,'text/plain')}
    url = "https://app1-apigw.central.arubanetworks.com/configuration/v1/groups/tec-temp/templates"
    querystring = {"name":"temp01","device_type":"IAP","access_token":access_token}

    response = requests.request("PATCH", url, params=querystring, files=files)
    print(response.text)


    if response.status_code == requests.codes.ok:
        speech_output = 'ゲストワイファイを無効にしました';
        reprompt_text ='もう一度お願いします';
        should_end_session = True

    else:
        speech_output = 'ゲストワイファイを無効にできませんでした。';
        reprompt_text ='もう一度お願いします';
        should_end_session = True

まとめ

今回は、GuestWiFiの有効/無効という設定変更をサンプルとして作成してみましたがAruba Centralには様々なAPIが用意されていますので色々と活用できそうです。
今回は、入力部分でAlexaを使用しましたがChatbotなどでも対応可能です。

参考

[API] Alexa と Aruba Central を使ったネットワークの音声管理
Aruba+Chatbot DEMO

14
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
2