0

After making a connection to a cisco device using python, and executing a command I get a output as shown below, Basically I want to get only one value, that the value for Services which is shown as 1 below and store it in a variable.

#show ethernet cfm domain brief
Domain Name                              Index Level Services Archive(min)
EVC                                          2     4        1     100

I'm not sure how to parse this, I tried converting to a list, but the list became very huge something like this and I dont know if the same will work on another cisco ios-xe device.

['Domain', 'Name', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Index', 'Level', 'Services', 'Archive(min)\r\nEVC', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', '4', '', '', '', '', '', '', '', '1', '', '', '', '', '100']

Is there any other reliable way to get the value of services which is 1 ?

mac
  • 863
  • 3
  • 21
  • 42

1 Answers1

0

Example how to use TextFSM to parse output of "show ethernet cfm domain brief"

Create template.textfsm template file with the following context

Value DOMAIN_NAME (\S+)
Value INDEX (\S+)
Value LEVEL (\S+)
Value SERVICES (\S+)
Value ARCHIVE (\S+)

Start
  ^Domain Name\s+Index\s+Level\s+Services\s+Archive\(min\)
  ^${DOMAIN_NAME}\s+${INDEX}\s+${LEVEL}\s+${SERVICES}\s+${ARCHIVE} -> Record

and use this template to parse your output

import textfsm

# show ethernet cfm domain brief
output = """
Domain Name                              Index Level Services Archive(min)
EVC                                          2     4        1     100
"""

with open("template.textfsm", "r") as f:
    template = textfsm.TextFSM(f)
    items = [dict(zip(template.header, row)) for row in template.ParseText(output)]
    print(items)  # [{'DOMAIN_NAME': 'EVC', 'INDEX': '2', 'LEVEL': '4', 'SERVICES': '1', ...

    for item in items:
        print(item["SERVICES"])  # 1
pyjedy
  • 389
  • 2
  • 9