LoginSignup
0
0

Cisco SD-WAN OCN MAP-E証明書の一括インストール

Last updated at Posted at 2023-12-20

はじめに

この記事は2023年CiscoSystems合同会社 社員有志 による Advent Calendar の記事の一つです。
残念ながらカレンダーに空きがなかったので個別に投稿しています。

この記事の目的

OCN MAP-E設定
上記サイトはCiscoSD-WANのMAP-E機能を使ってSD-WANルータをIPoE環境で構築する基本設定を紹介しています。この設定のうち、MAP-EのAddressサーバとのHTTPSアクセスのためのRoot証明書をルータにインストールする手順を簡素化するためのスクリプトを作成しました。

スクリーンショット 2022-12-07 11.41.39.png

Pythonスクリプトの使い方
vManageには必要なモジュールがすでにインストールされていますのでPIPインストール等は不要です。PCなどで使う時には必要に応じてインストールしてください。

  1. ここからRoot証明書をダウンロード
  2. vManageに必要なpython本体とRoot証明書をアップロード
  3. コマンド実行
vmanage#vshell
vmanage:~/$ python3 tools.py 8.1.1.1 admin admin

スクリプト本体

#!/usr/bin/python3
import os
import paramiko
import argparse
import time
import multiprocessing

def transfer_file_and_execute(router_ip, username, password):
    transfer_file(router_ip, username, password)
    connect_and_execute(router_ip, username, password)

def transfer_file(sip, loginid, loginpass):
    file_path = './TP.ca'
    remote_path = f'/bootflash/sdwan/TP.ca'
    command = f'sshpass -p {loginpass} scp -o StrictHostKeyChecking=no -P 830 {file_path} {loginid}@{sip}:{remote_path}'
    os.system(command)

def connect_and_execute(router_ip, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        ssh.connect(router_ip, port=830, username=username, password=password)
        shell = ssh.invoke_shell()

        shell.send("shell\n")
        time.sleep(1)
        output = ""
        while shell.recv_ready():
            output += shell.recv(1024).decode()

        if "password:" in output.lower():
            shell.send(f"{password}\n")

        shell.send("terminal length 0\n")
        time.sleep(1)
        output = ""
        while shell.recv_ready():
            output += shell.recv(1024).decode()

        shell.send("copy bootflash:sdwan/TP.ca bootflash:TP.ca\n")
        time.sleep(1)
        output += shell.recv(1024).decode()

        shell.send("\n")
        time.sleep(5)
        output_copy = ""
        while shell.recv_ready():
            output_copy += shell.recv(1024).decode()

        shell.send("crypto pki authenticate TP\n")
        time.sleep(3)
        output_authenticate = ""
        while shell.recv_ready():
            output_authenticate += shell.recv(1024).decode()

        print(f"Output of 'copy' command for {router_ip}:\n", output_copy)
        print(f"Output of 'crypto pki authenticate' command for {router_ip}:\n", output_authenticate)

        ssh.close()

    except paramiko.AuthenticationException:
        print("Authentication failed. Please check your credentials.")
    except paramiko.SSHException as ssh_ex:
        print(f"Error while connecting: {ssh_ex}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Connect to Cisco SD-WAN router and execute commands.")
    parser.add_argument("routers", nargs='+', help="The router IP addresses")
    parser.add_argument("username", help="The router username")
    parser.add_argument("password", help="The router password")
    args = parser.parse_args()

    processes = []
    for router_ip in args.routers:
        process = multiprocessing.Process(target=transfer_file_and_execute, args=(router_ip, args.username, args.password))
        processes.append(process)
        process.start()

    for process in processes:
        process.join()

実行結果

vmanage:~/star$ python3 tools.py 8.1.1.1 admin admin
Output of 'copy' command:

Copy in progress...C
1468 bytes copied in 0.011 secs (133455 bytes/sec)
c8kv-001#

確認コマンド
show crypt pkt trustpoint TP

ルータ上であらかじめ設定が必要なConfig

crypto pki trustpoint TP
 enrollment url bootflash:
 fingerprint      AD7E1C28B064EF8F6003402014C3D0E3370EB58A
 revocation-check none
!

参考文献

0
0
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
0
0