LoginSignup
17
15

More than 5 years have passed since last update.

ルータコンフィグ収集ツールConfigCollecor作りました

Last updated at Posted at 2015-07-15

概要

以前投稿したルータコンフィグ収集ツールを修正して、PyPIで公開しました。
Cisco/Juniperルータのコンフィグを一元取得するツール

https://pypi.python.org/pypi/ConfigCollector
https://github.com/taijiji/ConfigCollector

初めてのPyPI登録だったので手間取りましたが、なんとかPyPIデビューすることができました。

モチベーション

ルータコンフィグをGitで管理したかったのですが、Gitの前に、運用しているルータ群のコンフィグのコンフィグを集めるのが手間だったので、1コマンドで全ルータのコンフィグ収集ができるConfigCollectorを作りました。

Gitコマンドも含めて自動化しようとも考えたのですが、「confilctした場合は人手で取捨選択したほうがよい」「機密情報であるルータコンフィグを強制アップロードさせると操作を間違った時にトラブルが出そう」「エラー時にgit removeなどのオプション機能を実装するのがめんどくさかった(実装が面倒な割に、ニーズがあるかよくわからない)」といった理由から、本ツールのターゲットは「複数のルータに対してSSHログインしてコンフィグを取得
・収集する」ことに限定しています。

インストール

PyPIで公開したことで、pipコマンドでインストールできるようになりました。

pip install configcolletctor

pipを使ったことがない方はこちらの記事を参考にpipをインストールしてください。
Pythonで一番最初に入れるべきパッケージ setuptools と pip

使い方

インストール後、このようにしてツールを動かします。

configcollector -i [inventory_file] -o [output_dirctory]
  • inventory_file
    • ルータ情報をJson形式で記述します。
    • ルータOSは下記の中から選びます。
      • JUNOS
      • IOS
      • IOS-XE
      • IOS-XR
    • inventory_file記述例
[
    {
        "hostname" : "router1",
        "username" : "user1",
        "password" : "aaabbbccc",
        "ipv4"     : "192.168.0.1",
        "os"       : "JUNOS"
    },
    {
        "hostname" : "router2",
        "username" : "user2",
        "password" : "aaabbbccc",
        "ipv4"     : "192.168.0.2",
        "os"       : "IOS-XR"
    },
    {
        "hostname" : "router3",
        "username" : "user3",
        "password" : "aaabbbccc",
        "ipv4"     : "192.168.0.3",
        "os"       : "IOS"
    }
]
  • output_directory
    取得したルータコンフィグが出力されるディレクトリです。
    デフォルトでは、カレントディレクリが指定されます。

使用例

ConfigCollectorは下記のようにして使います。

まずルータコンフィグが保存される出力先のディレクトリを作成します。

    % mkdir router_config

ConfigCollectorを実行します

    % configcollector -i sample_routers.json -o router_config/

    Accessing router: router1...
    Writing output file "router_config/router1"...
    Success : "router_config/router1"!
    Accessing router: router2...
    Writing output file "router_config/router2"...
    Success : "router_config/router2"!
    Accessing router: router3...
    Writing output file "router_config/router3"...
    Success : "router_config/router3"!

作成されたファイルを確認します。

    % ls router_config
    router1.txt  router2.txt  router3.txt
    % less router1.txt

    show configuration | no-more
    ## Last commit: 2015-05-01 17:00:00 JST by user1
    version x.x.x;
    system {
        host-name router1;
        time-zone Asia/Tokyo;
    (snip)

以上で完了です。

本ツールを使ってルータコンフィグをGit管理する

本ツールで取得したルータコンフィグを以下の手順でGit管理することができます。

1.Git管理サーバを使ってルータコンフィグ用レポジトリを作成
2.ルータコンフィグの取得

 % configcollector -i sample_routers.json -o router_config/

3.git ステージングに追加 (更新候補として指定)

 % git add router_config/ . 

4.git コミット (更新を実行)

 % git commit 

5.git リモートレポジトリにアップロード (Git管理サーバにアップロード)

 % git push
 (初回のみ % git push origin master)

6.Git管理サーバで確認

所感

人生で初めてPyPIに登録しました。はじめてまともなOSSコミッターの仲間入りができたような気持ちで嬉しい限りです。
まだPyPIやOSSのお作法がわかっていないところが多分にあるので、ぜひフィードバックいただけると嬉しいです。
https://pypi.python.org/pypi/ConfigCollector
https://github.com/taijiji/ConfigCollector

17
15
4

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
17
15