0

would like to get some help over here for using Cisco Genie parser. Is it possible to load the output of the CLI command (eg. "show version") into the Genie parser.

My customer pass me the output of "show version" for each of their device. I have no ssh access to their devices for security reason. I'm able to extract the output from a Python script.

But how do I load the CLI output to the Genie parser? Usually what I did is below, but this only applicable if I have ssh connection to the device:

output = device.parse("show version")

So how do I load a output string to the parse and tell it which parser to use?? I'm puzzle...

Michael
  • 197
  • 1
  • 2
  • 10

2 Answers2

0

You can take the following example, here CLI is for "show interface" command:

from genie.libs.parser.ios.show_interface import ShowInterfaces

parser = ShowInterfaces(device= '', context='cli')
parsed_dict = parser.cli(output=str_op)

Here, str_op is the output from CLI command in string format

0

If you don't have SSH access, I can recommend the TTP module. After adding the CLI output to a notepad, you can write your own template. You can easily parse the data you want. I have given an example below.(show users)

Example Code:

from pprint import pprint
from ttp import ttp
import json
import time
            
with open("showUsers.txt") as f:
    data_to_parse = f.read()
            
ttp_template = """
<group name="showUsers" method="table">
{{User|re(".?")|re(".*")}}                             {{Type}}      {{Login_Date}} {{Login_Time}}           {{Idle_day}} {{Idle_time}} --
              {{Session_ID}}   {{From}}
            </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:

[
                {
                    "showUsers": [
                        {
                            "From": "--",
                            "Session_ID": "6"
                        },
                        {
                            "Idle_day": "0d",
                            "Idle_time": "00:00:00",
                            "Login_Date": "08FEB2022",
                            "Login_Time": "10:53:29",
                            "Type": "SSHv2",
                            "User": "admin"
                        },
                        {
                            "From": "135.244.199.185",
                            "Session_ID": "132"
                        },
                        {
                            "Idle_day": "0d",
                            "Idle_time": "00:03:35",
                            "Login_Date": "09FEB2022",
                            "Login_Time": "11:32:50",
                            "Type": "SSHv2",
                            "User": "admin"
                        },
                        {
                            "From": "10.144.208.82",
                            "Session_ID": "143"
                        }
                    ]
                }
            ]
Mert Kulac
  • 294
  • 2
  • 19