简体   繁体   中英

TextFSM Template for Netmiko for "inc" phrase

I am trying to create a textfsm template with the Netmiko library. While it works for most of the commands, it does not work when I try performing "inc" operation in the.network device. The textfsm index file seems like it is not recognizing the same command for 2 different templates; for instance:

  1. If I am giving the command - show running | inc syscontact
  2. And give another command - show running | inc syslocation

in textfsm index; the textfsm template seems like it is recognizing only the first command; and not the second command.

I understand that I can get the necessary data by the regex expression for syscontact and syslocation for the commands( via the template ), however I want to achieve this by the "inc" command from the device itself. Is there a way this can be done?

you need to escape the pipe in the index file. eg sh[[ow]] ru[[nning]] \| inc syslocation

There is a different way to parse that you want all datas which is called TTP module. You can take the code I wrote below as an example. You can create your own templates.

from pprint import pprint
from ttp import ttp
import json
import time
                
with open("showSystemInformation.txt") as f:
   data_to_parse = f.read()

ttp_template = """
<group name="Show_System_Information">
System Name            : {{System_Name}}
System Type            : {{System_Type}} {{System_Type_2}}
System Version         : {{Version}}
System Up Time         : {{System_Uptime_Days}} days, {{System_Uptime_HR_MIN_SEC}} (hr:min:sec)
Last Saved Config      : {{Last_Saved_Config}}
Time Last Saved        : {{Last_Time_Saved_Date}} {{Last_Time_Saved_HR_MIN_SEC}}
Time Last Modified     : {{Last_Time_Modified_Date}} {{Last_Time_Modifed_HR_MIN_SEC}}
</group>
"""
                
parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]
print(results)

Example run:

[appadmin@ryugbz01 Nokia]$ python3 showSystemInformation.py
[
    {
        "Show_System_Information": {
            "Last_Saved_Config": "cf3:\\config.cfg",
            "Last_Time_Modifed_HR_MIN_SEC": "11:46:57",
            "Last_Time_Modified_Date": "2022/02/09",
            "Last_Time_Saved_Date": "2022/02/07",
            "Last_Time_Saved_HR_MIN_SEC": "15:55:39",
            "System_Name": "SR7-2",
            "System_Type": "7750",
            "System_Type_2": "SR-7",
            "System_Uptime_Days": "17",
        "System_Uptime_HR_MIN_SEC": "05:24:44.72",
            "Version": "C-16.0.R9"
        }
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM