シスコルータのコンフィグ作成をPythonで自動化してみた!
2021.02.26
シスコルータのコンフィグ作成をPythonで自動化してみよう!
シスコルータのコンフィグ作成を、「仕事で任された!」という方も、中にはいるかもしれません。
ざっくり言ってしまえば、コンフィグ内容は、主にポートと VLAN の「追加・削除」となります。
過去のコンフィグを、コピぺして編集するのも良いと思います。
ですが、よく出てくる案件なので、できる限り自動化を目指したいですよね?
そこで今回は、ルーターの VLAN 追加のコンフィグ作成を、Python を使って自動化してみたいと思います!
こちらの記事もオススメ!
実行環境
今回は、以下のような環境で、シスコルータのコンフィグ作成を自動化していきます。
- OS : Mac OS X 10.15.6
- Ciscoシミュレータ : Packet Tracer 7.3
- Pythonバージョン : 3.8.6
では早速、はじめていきましょう!
既存ネットワークを構築する
既存ネットワークとして、以下のようなネットワークを構成します。
PC0 は VLAN10 、PC1 は VLAN20 と分かれており、Router1 と Switch は、トランクリンクで接続されています。
PC0の設定
PC0 の設定は、次の通りです。
- IP : 192.168.0.1/24
- DG : 192.168.0.254
PC1の設定
PC1 の設定は、次のようにしています。
- IP : 192.168.1.1/24
- DG : 192.168.1.254
Switch1の設定(コンフィグ)
以下のコンフィグを、テキストファイルに貼り付けて、Switch1 に投入していきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ! hostname Switch1 ! interface FastEthernet0/1 switchport access vlan 10 switchport mode access ! interface FastEthernet0/2 switchport access vlan 20 switchport mode access ! interface FastEthernet0/3 switchport access vlan 30 switchport mode access ! interface FastEthernet0/4 switchport access vlan 40 switchport mode access ! interface GigabitEthernet0/1 switchport trunk native vlan 100 switchport mode trunk switchport nonegotiate ! |
Router1の設定(コンフィグ)
次に、以下のコンフィグもテキストファイルに貼り付けて、Router1に投入しておきましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ! hostname Router1 ! interface GigabitEthernet0/0/0.10 encapsulation dot1Q 10 ip address 192.168.0.254 255.255.255.0 ! interface GigabitEthernet0/0/0.20 encapsulation dot1Q 20 ip address 192.168.1.254 255.255.255.0 ! interface GigabitEthernet0/0/0.100 encapsulation dot1Q 100 native ip address 192.168.100.254 255.255.255.0 ! |
疎通確認
ここまでで、PC0 から PC1 に、ping が通るか確認してみましょう!
以下のような、コマンドを叩いて、確認してみてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 | C:\>ping 192.168.1.1 Pinging 192.168.1.1 with 32 bytes of data: Reply from 192.168.1.1: bytes=32 time<1ms TTL=127 Reply from 192.168.1.1: bytes=32 time=1ms TTL=127 Reply from 192.168.1.1: bytes=32 time<1ms TTL=127 Reply from 192.168.1.1: bytes=32 time<1ms TTL=127 Ping statistics for 192.168.1.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 1ms, Average = 0ms |
通りましたね!
変更後ネットワーク
新たに、PC2 と PC3 の2台の PC が、Switch1 に接続されました。
PC2 と PC3 が接続する Switch1 のポートには、それぞれ「VLAN30」「VLAN40」が設定されています。
PC2の設定
PC2 の設定は、次の通りです。
- IP : 192.168.2.1/24
- DG : 192.168.2.254
PC3の設定
PC3 の設定は、以下のようになっています。
- IP : 192.168.3.1/24
- DG : 192.168.3.254
疎通確認
PC0 から、PC2 と PC3 に ping を打ってみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | C:\>ping 192.168.2.1 Pinging 192.168.2.1 with 32 bytes of data: Reply from 192.168.0.254: Destination host unreachable. Reply from 192.168.0.254: Destination host unreachable. Reply from 192.168.0.254: Destination host unreachable. Request timed out. Ping statistics for 192.168.2.1: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss), C:\>ping 192.168.3.1 Pinging 192.168.3.1 with 32 bytes of data: Reply from 192.168.0.254: Destination host unreachable. Reply from 192.168.0.254: Destination host unreachable. Reply from 192.168.0.254: Destination host unreachable. Reply from 192.168.0.254: Destination host unreachable. Ping statistics for 192.168.3.1: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss), C:\> |
ですが現状では、期待通りに通信ができませんね…。
これは、ルータのトランクリンクに、VLAN の追加をしていないためです。
シスコルータのコンフィグを自動作成
ここでは、Router1 に「VLAN30」「VLAN40」を追加するコンフィグを、Python を使って作成してみます。
順に進めていきましょう!
template.txtにテンプレートを作成する
まず template.txt に、以下のようなテンプレートを作成します。
1 2 3 4 5 | ! interface GigabitEthernet$port.$vlan $no encapsulation dot1Q $vlan $no ip address $ip ! |
「$変数名」の部分が、パラメータと置き換えられ、コンフィグが作成されます。
param.json にパラメータを定義する
param.json とした json ファイルに、パラメータを定義しましょう。
先ほど作成した、template.txt の変数部分が、以下のパラメータに置き換えられます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [ { "host":"Router1", "ip":"192.168.2.254/24", "vlan":"30", "port":"0/0/0", "no":"" }, { "host":"Router1", "ip":"192.168.3.254/24", "vlan":"40", "port":"0/0/0", "no":"" } ] |
generate_route_config.py
generate_route_config.py に、以下のような処理を記述して、シスコルータのコンフィグを自動作成していきましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #!/usr/bin/python3 from string import Template import json import ipaddress # jsonを読み込む with open('./param.json', "r") as f: json_ary = json.load(f) # テンプレートを読み込む with open('./template.txt', "r") as f: tmpl = Template(f.read()) # 設定するVLANの数だけループ for json_data in json_ary: # ipアドレス情報を扱うオブジェクトを設定 ip_obj = ipaddress.ip_interface(json_data['ip']) # テンプレート内の変数を該当のパラメータに置換する config = tmpl.substitute( json_data, # IPアドレスとサブネットマスクを設定 ip = ip_obj.with_netmask.replace('/', ' '), # VLANを設定 vlan = json_data['vlan'], # PORTを設定 port = json_data['port'], # パラメータにnoを設定することで設定削除が可能。(今回は追加のため空白) no = json_data['no'] ) # ホスト名でファイルを書き込む with open(json_data['host'] + '.txt', "a") as f: f.write(config) |
まず、パラメータが定義された json ファイルを読み込む処理が記述されています。
その後は、テンプレートの変数部分を置換し、コンフィグを自動作成する処理です。
詳しくは、中のコードを追いながら、確認してみてくださいね!
実行後コンフィグパッチ
generate_route_config.py と同じ場所に、param.json と template.txt を配置し実行します。
1 2 3 4 5 6 | $ ls generate_route_config.py param.json template.txt $ python generate_route_config.py $ ls Router1.txt param.json generate_route_config.py template.txt |
すると、Router1.txt が作成されたかと思います。
1 2 3 4 5 6 7 8 9 10 | ! interface GigabitEthernet0/0/0.30 encapsulation dot1Q 30 ip address 192.168.2.254 255.255.255.0 ! ! interface GigabitEthernet0/0/0.40 encapsulation dot1Q 40 ip address 192.168.3.254 255.255.255.0 ! |
コンフィグを投入して疎通確認
作成された Router1.txt を、Router1 に投入していきましょう。
すると、以下のように「VLAN30」「VLAN40」の設定が追加されるはずです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ! interface GigabitEthernet0/0/0.10 encapsulation dot1Q 10 ip address 192.168.0.254 255.255.255.0 ! interface GigabitEthernet0/0/0.20 encapsulation dot1Q 20 ip address 192.168.1.254 255.255.255.0 ! interface GigabitEthernet0/0/0.30 encapsulation dot1Q 30 ip address 192.168.2.254 255.255.255.0 ! interface GigabitEthernet0/0/0.40 encapsulation dot1Q 40 ip address 192.168.3.254 255.255.255.0 ! interface GigabitEthernet0/0/0.100 encapsulation dot1Q 100 native ip address 192.168.100.254 255.255.255.0 ! |
疎通確認
最後に、以下のようにコマンドを叩いて、ping が通るか確認してみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | C:\>ping 192.168.2.1 Pinging 192.168.2.1 with 32 bytes of data: Reply from 192.168.2.1: bytes=32 time=1ms TTL=127 Reply from 192.168.2.1: bytes=32 time<1ms TTL=127 Reply from 192.168.2.1: bytes=32 time<1ms TTL=127 Reply from 192.168.2.1: bytes=32 time=1ms TTL=127 Ping statistics for 192.168.2.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 1ms, Average = 0ms C:\>ping 192.168.3.1 Pinging 192.168.3.1 with 32 bytes of data: Reply from 192.168.3.1: bytes=32 time=32ms TTL=127 Reply from 192.168.3.1: bytes=32 time<1ms TTL=127 Reply from 192.168.3.1: bytes=32 time<1ms TTL=127 Reply from 192.168.3.1: bytes=32 time=1ms TTL=127 Ping statistics for 192.168.3.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 32ms, Average = 8ms |
PC0 から PC2 と PC3 に、ping が通ることが確認できましたね!
さいごに
今回は、VLAN 追加のコンフィグを、Python で作成しました。
ただ、実際の現場では、もっと設定項目が多いものです。
だからこそ、煩雑なコンフィグ作成を自動化することは、とても効率が上がるわけですね!
豊富なライブラリのある Python を使えば、あらゆる業務を自動化できます。
随所で工夫をして、ぜひ業務の生産性を高めていってくださいね!
記事を書いた人
システム開発依頼・お見積もりはこちらまでお願いします。
また、WEB・スマホ系エンジニアを積極採用中です!
こちらの記事もオススメ!
書いた人はこんな人
- 「好きを仕事にするエンジニア集団」の(株)ライトコードです!
ライトコードは、福岡、東京、大阪の3拠点で事業展開するIT企業です。
現在は、国内を代表する大手IT企業を取引先にもち、ITシステムの受託事業が中心。
いずれも直取引で、月間PV数1億を超えるWebサービスのシステム開発・運営、インフラの構築・運用に携わっています。
システム開発依頼・お見積もり大歓迎!
また、現在「WEBエンジニア」「モバイルエンジニア」「営業」「WEBデザイナー」「WEBディレクター」を積極採用中です!
インターンや新卒採用も行っております。
以下よりご応募をお待ちしております!
https://rightcode.co.jp/recruit
- ライトコードの日常2月 29, 2024座談会はじめました!ライトコードの話ちょっと聞いてみませんか?
- ライトコードの日常12月 27, 2023年忘れ!ライトコード大忘年会2023
- ライトコードの日常12月 1, 2023ライトコードクエスト〜東京オフィス歴史編〜
- ITエンタメ10月 13, 2023Netflixの成功はレコメンドエンジン?