Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu
Netmiko TEXTFSM

How to Install and Parse data with Netmiko TextFSM Plugin

Posted on August 8, 2021September 1, 2021 by Gurpreet Kochar

The foremost is to be setup TextFSM correctly for Netmiko to work. The official site (Netmiko & NTC )explains a method that didn’t really work for me. I have tried to set up TextFSM using pip install ntc_templates and also using git clone but none of them particularly worked for me out of the box. This method below is what I have found to work most reliably. Let’s see how to parse data with the netmiko text plugin.

STEP1:-

In whichever directly you have your netmiko script, git clone the ntc-templates repository.

Step2:- Tell Netmiko where to find the textfsm templates.

from netmiko import ConnectHandler
from pprint import pprint
import os

# set templates directory to the path where index file is located. If you open templates directory
# there is an index file which is what netmiko is looking to find to work properly.
templates = os.path.dirname(os.path.abspath(__file__)) + '/ntc-templates/ntc_templates/templates'
os.environ['NET_TEXTFSM']= templates

# set the environment variable NET_TEXTFSM to this directory which is again what Netmiko expects

Step3:- Modify the send_command( ) method you are using to add use_textfsm=True as an argument

from netmiko import ConnectHandler
from pprint import pprint
import os

templates = os.path.dirname(os.path.abspath(__file__)) + '/ntc-templates/ntc_templates/templates'
os.environ['NET_TEXTFSM']= templates

csr1000v1 = {
    'device_type': 'cisco_ios',
    'host':   'sandbox-iosxe-latest-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,                # optional, defaults to 22
    'secret': 'C1sco12345',     # optional, defaults to ''
}
csr1000v2 = {
    'device_type': 'cisco_ios',
    'host':   'sandbox-iosxe-recomm-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,
    'secret': 'C1sco12345',
}
iosxrv9000 = {
    'device_type': 'cisco_xr',
    'host':   'sandbox-iosxr-1.cisco.com',
    'username': 'admin',
    'password': 'C1sco12345',
    'port' : 22,
    'secret': 'C1sco12345',
}
nxosv9000 = {
    'device_type': 'cisco_nxos',
    'host':   'sandbox-nxos-1.cisco.com',
    'username': 'admin',
    'password': 'Admin_1234!',
    'port' : 22,
    'secret': 'Admin_1234!',
}


all_devices = [csr1000v1, csr1000v2, iosxrv9000, nxosv9000]
for device in all_devices:
    net_connect = ConnectHandler(**device)
    print(net_connect.host)
    print(net_connect.send_command('show ver', use_textfsm=True)) ## use_textfsm=True



╰─ python3 script5.py                                                                                                                                                                      ─╯
sandbox-iosxe-latest-1.cisco.com
[{'version': '17.3.1a', 'rommon': 'IOS-XE', 'hostname': 'csr1000v-1', 'uptime': '1 day, 5 hours, 22 minutes', 'uptime_years': '', 'uptime_weeks': '', 'uptime_days': '1', 'uptime_hours': '5', 'uptime_minutes': '22', 'reload_reason': 'reload', 'running_image': 'packages.conf', 'hardware': ['CSR1000V'], 'serial': ['9ESGOBARV9D'], 'config_register': '0x2102', 'mac': [], 'restarted': ''}]

sandbox-iosxe-recomm-1.cisco.com
[{'version': '16.9.3', 'rommon': 'IOS-XE', 'hostname': 'csr1000v-1', 'uptime': '2 days, 20 hours, 8 minutes', 'uptime_years': '', 'uptime_weeks': '', 'uptime_days': '2', 'uptime_hours': '20', 'uptime_minutes': '8', 'reload_reason': 'reload', 'running_image': 'packages.conf', 'hardware': ['CSR1000V'], 'serial': ['926V75BDNRJ'], 'config_register': '0x2102', 'mac': [], 'restarted': ''}]

sandbox-iosxr-1.cisco.com
[{'version': '6.5.3', 'uptime': '2 weeks 1 day 23 hours 28 minutes', 'location': '/opt/cisco/XR/packages/', 'hardware': 'IOS-XRv 9000', 'build_host': 'iox-ucs-019'}]

sandbox-nxos-1.cisco.com
[{'uptime': '10 day(s), 23 hour(s), 35 minute(s), 44 second(s)', 'last_reboot_reason': 'Unknown', 'os': '9.3(3)', 'boot_image': 'bootflash:///nxos.9.3.3.bin', 'platform': 'C9300v', 'hostname': 'sbx_nxosv1', 'serial': '9N3KD63KWT0'}]

IMPORTANT:- It is advisable to use the full command name while using any parser plugin like textfsm, ttp, or genie. While certain well-known commands may work with abbreviations with parsers like textfsm understands show version and show ver, other commands might not work.

To make the data look neat, we can convert the output into JSON format simply by below modifications

import json ## add this to other imports


all_devices = [csr1000v1, csr1000v2, iosxrv9000, nxosv9000]
for device in all_devices:
    net_connect = ConnectHandler(**device)
    output = net_connect.send_command('show version', use_textfsm=True)
    print(json.dumps(output, indent=4, sort_keys=True)) ## convert the return data into JSON


[
    {
        "config_register": "0x2102",
        "hardware": [
            "CSR1000V"
        ],
        "hostname": "csr1000v-1",
        "mac": [],
        "reload_reason": "reload",
        "restarted": "",
        "rommon": "IOS-XE",
        "running_image": "packages.conf",
        "serial": [
            "9ESGOBARV9D"
        ],
        "uptime": "1 day, 7 hours, 42 minutes",
        "uptime_days": "1",
        "uptime_hours": "7",
        "uptime_minutes": "42",
        "uptime_weeks": "",
        "uptime_years": "",
        "version": "17.3.1a"
    }
]
[
    {
        "config_register": "0x2102",
        "hardware": [
            "CSR1000V"
        ],
        "hostname": "csr1000v-1",
        "mac": [],
        "reload_reason": "reload",
        "restarted": "",
        "rommon": "IOS-XE",
        "running_image": "packages.conf",
        "serial": [
            "926V75BDNRJ"
        ],
        "uptime": "2 days, 22 hours, 28 minutes",
        "uptime_days": "2",
        "uptime_hours": "22",
        "uptime_minutes": "28",
        "uptime_weeks": "",
        "uptime_years": "",
        "version": "16.9.3"
    }
]
[
    {
        "build_host": "iox-ucs-019",
        "hardware": "IOS-XRv 9000",
        "location": "/opt/cisco/XR/packages/",
        "uptime": "2 weeks 2 days 1 hour 48 minutes",
        "version": "6.5.3"
    }
]
[
    {
        "boot_image": "bootflash:///nxos.9.3.3.bin",
        "hostname": "sbx_nxosv1",
        "last_reboot_reason": "Unknown",
        "os": "9.3(3)",
        "platform": "C9300v",
        "serial": "9N3KD63KWT0",
        "uptime": "11 day(s), 1 hour(s), 55 minute(s), 45 second(s)"
    }
]

All the heavy lifting of writing a custom regular expression to catch all matches coupled with the fact that you also take multiple platforms and output formats into account. All that is left to be done now is put it into a well-formatted excel sheet for later reference. See other posts on how to work with CSV and EXCEL files with python for network automation engineers.

How to read write csv with python for Network Automation Part 1
https://networkautomationlane.in/how-to-read-write-csv-with-python-for-network-automation-part-1/
How to read write csv with python for Network Automation Part 2
https://networkautomationlane.in/how-to-read-write-csv-with-python-for-network-automation-part-2/
How to read write Excel with python for Network Automation Part 1
https://networkautomationlane.in/how-to-read-write-excel-with-python-for-network-automation-part-1/

Use vanilla TextFsm

How to Parse data with TextFSM

Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

1 thought on “How to Install and Parse data with Netmiko TextFSM Plugin”

  1. Pingback: How to Parse data with TextFSM – Network Automation

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...
 

    %d