LoginSignup
5
4

More than 3 years have passed since last update.

Cisco機器の設定変更をBotで通知させた時のメモ

Last updated at Posted at 2019-05-12

はじめに

前回と同じくEEMとOn-Box Pythonを組み合わせ、設定変更をAPI経由でWebex Teams Botへ通知させてみました。

手順は以下を参考にし、変更部分をメモとして残しています。
POC: Catalyst 9000 Sandbox integration with Spark.

1) 用意した環境

AWS MarketplaceのCSR1000Vを使用しました。
Cisco Cloud Services Router (CSR) 1000V - AX Pkg. Max Performance

30日間はFree Trial期間のため、CSRのソフトウェア費用はかかりません。
ただしEC2利用料はかかります。
バージニアリージョンのEC2(t2.medium)の場合、0.046ドル/時間でした。
無題5.png
EC2起動後、セキュリティグループでSSHを許可し、SSHログインします。

2) Webex Teams Botの作成

Cisco Webex for Developersにログインし、[Start Building Apps] > [Create a New App] > [Create a Bot] にてBotを作成します。
詳しい手順は2)の通りです。

3) ゲストシェルのセットアップ

詳しい手順は3)の通りです。
ただしAWSの場合、DNSサーバの設定変更は不要です。

4) Pythonスクリプトの作成とBotのテスト

基本的に4)と同じですが、対象ホスト名と変更内容をBotに表示させるコードを追加しています。
具体的には、get_hostnameメソッドでホスト名を取得し、diffiosモジュールでStartup ConfigとRunning Configの差分を抽出しています。
別途ignore.txtを作成し、無視して構わない差分を定義しています。

myscript.py
from ciscosparkapi import CiscoSparkAPI
from cli import cli
import diffios


def get_hostname():
    sh_run_host = cli('show run | include hostname')
    host_strip = sh_run_host.strip()
    hostname = host_strip.lstrip('hostname ')
    return hostname


if __name__=='__main__':
    # Get hostname of device
    hostname = get_hostname()

    # Get diffs of startup-config and running-config
    with open('sh_start.log', 'w') as f1:
        sh_start = cli('show startup-config')
        f1.write(sh_start)

    with open('sh_run.log', 'w') as f2:
        sh_run = cli('show running-config')
        f2.write(sh_run)

    baseline = 'sh_start.log'
    comparison = 'sh_run.log'
    ignore = 'ignore.txt'

    diff = diffios.Compare(baseline, comparison, ignore)
    print diff.delta()

    # Use ArgParse to retrieve command line parameters.
    from argparse import ArgumentParser

    parser = ArgumentParser("Spark Check In")

    # Retrieve the Spark Token and Destination Email
    parser.add_argument(
        "-t", "--token", help="Spark Authentication Token", required=True
    )

    # Retrieve the Spark Token and Destination Email
    parser.add_argument(
        "-e", "--email", help="Email to Send to", required=True
    )
    args = parser.parse_args()
    token = args.token
    email = args.email
    message = "**Alert:** Config Changed  \n**Hostname:** " + hostname + "  \n```\n" + diff.delta() + "\n```"
    api = CiscoSparkAPI(access_token=token)
    api.messages.create(toPersonEmail=email, markdown=message)
ignore.txt
^Using
Building configuration
Current configuration
crypto pki certificate
^end$

5) NW機器側のEEM設定

5)と同じです。アクセストークンとメールアドレスは用意したものに書き換えます。

変更箇所をBotで表示させるために、一旦設定保存した後、以下の設定変更を行いました。

conf t
ip domain lookup
ip access-list extended Test
 permit ip host 10.10.10.1 host 10.10.10.2

Webex Teamsにログインし、Botからの通知を確認します。
対象のホスト名、変更箇所が表示されました!
無題21.png

5
4
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
5
4