Catalyst2960L + Netmiko + TextFSMを使ってみる



Catalyst2960LにSSHで接続して、sh verするところまでの備忘録です。環境構築については取り扱いません。というか、コードと実行結果を載せてるだけで解説はほぼないです。





 ┃ ┗
 ┗ devicve_conf
   ┗ cisco_device.json


Cisco VIRLのIOSを操作するための定義もありますが、今回は使用しません。catalystの定義のみ使います。

    "catalyst" : {
        "ip" : "",
        "port" : "22",
        "device_type" : "cisco_ios_ssh",
        "username" : "admin",
        "password" : "your_password",
        "secret" : "your_password"
    "virl_ios_telnet" : {
        "ip" : "",
        "port" : "17001",
        "device_type" : "cisco_ios_telnet",
        "username" : "guest",
        "password" : "guest",
        "secret" : "cisco"


特に難しいようなところは無いかと思います。Catalyst だと取得できている項目も、Cisco VIRL上のIOSvだと取得できない項目もあります。この辺りは、NTC TEMPLATESを弄るしかなさそうですが、勉強不足につき後回し。

from netmiko import ConnectHandler
import json
from pathlib import Path
from pprint import pprint

def connect(device):
    with ConnectHandler(**device) as conn:
        # 特権Execモードに移行
        # show versionコマンドを実行し、TextFsmでParse 
        version = conn.send_command('sh ver', use_textfsm=True)

        # Parse結果の構造を確認 
        # 個々の値を取得してみる
        print("{:17}{}".format("hardware : ", version[0]['hardware']))
        print("{:17}{}".format("hostname : ", version[0]['hostname']))
        print("{:17}{}".format("running image : ", version[0]['running_image']))
        print("{:17}{}".format("uptime : ", version[0]['uptime']))
        print("{:17}{}".format("version : ", version[0]['version']))

if __name__ == "__main__":
    file = Path(__file__).resolve().parent / 'device_conf' / 'cisco_device.json'
    devices = json.load(open(file, 'r'))


$ python3
[{'config_register': '0xF',
  'hardware': ['WS-C2960L-8TS-LL'],
  'hostname': 'C2960-L',
  'mac': ['**:**:**:**:**:**'],
  'reload_reason': 'power-on',
  'rommon': 'Bootstrap',
  'running_image': 'c2960l-universalk9-mz.152-5c.E/c2960l-universalk9-mz.152-5c.E.bin',
  'serial': ['**********'],
  'uptime': '2 weeks, 4 hours, 26 minutes',
  'version': '15.2(5c)E'}]
hardware :       ['WS-C2960L-8TS-LL']
hostname :       C2960-L
running image :  c2960l-universalk9-mz.152-5c.E/c2960l-universalk9-mz.152-5c.E.bin
uptime :         2 weeks, 4 hours, 31 minutes
version :        15.2(5c)E

Catalyst 2960Lでsh verした結果は以下です。

C2960-L#sh ver
Cisco IOS Software, C2960L Software (C2960L-UNIVERSALK9-M), Version 15.2(5c)E, RELEASE SOFTWARE (fc1)
Technical Support:
Copyright (c) 1986-2016 by Cisco Systems, Inc.
Compiled Tue 18-Oct-16 23:59 by prod_rel_team

ROM: Bootstrap program is HAYWARDS boot loader
BOOTLDR: C2960L Boot Loader (C2960L-HBOOT-M) Version 15.2(5r)E1, RELEASE SOFTWARE (fc2)

C2960-L uptime is 2 weeks, 4 hours, 52 minutes
System returned to ROM by power-on
System restarted at 01:40:02 UTC Sun Jan 12 2020
System image file is "flash:c2960l-universalk9-mz.152-5c.E/c2960l-universalk9-mz.152-5c.E.bin"
Last reload reason: power-on

This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:

If you require further assistance please contact us by sending email to

cisco WS-C2960L-8TS-LL (Marvell PJ4B (584) v7 (Rev 2)) processor (revision D0) with 524288K bytes of memory.
Processor board ID FCW2111A5JP
Last reset from Reload
6 Virtual Ethernet interfaces
10 Gigabit Ethernet interfaces
The password-recovery mechanism is enabled.

512K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address       : **:**:**:**:**:**
Motherboard assembly number     : U58O342T06
Power supply part number        : ********
Motherboard serial number       : ********
Power supply serial number      : ********
Model revision number           : D0
Motherboard revision number     : 16
Model number                    : WS-C2960L-8TS-LL
System serial number            : ********
Top Assembly Part Number        : 74-105851-02
Top Assembly Revision Number    : A0
Version ID                      : V02
CLEI Code Number                : CMMWN00ARB
Hardware Board Revision Number  : 0x01

Configuration register is 0xF