having a problem using ttp while parsing a data which has similar template

330 views Asked by At

I am trying to parse following sample configurations located in the same file. I have two different service in which interfaces configured. I need to get each interfaces from the each service. However, as following services' interfaces have similar templates, when try to parse vprn interfaces, I am also getting ies interfaces. And vice versa.

      ies 10102 customer 1 create
          interface "Y" create
              description X
              address 192.168.19.134/30
              sap 1/3/7:3500.22 create
              exit
          exit

      vprn 10101 customer 1 create
          interface X create
              description X
              address 192.168.19.130/30
              sap 1/3/7:3500.21 create
              exit
          exit

I did find a workaround to get interfaces configured under each service using ttp. But, I would like to know if there is a way to parse data using start and end with multiple lines.

For example:

  {{_init_}}ies {{id}} customer {{id2}} create
      interface {{interface_name}} create
          address {{address|PREFIX}}
          sap {{sap_port}} create
          exit{{_end_}}
      exit
1

There are 1 answers

3
apraksim On BEST ANSWER

maybe someting like this would work

Data:

  ies 10102 customer 1 create
      interface "Y" create
          description X
          address 192.168.19.134/30
          sap 1/3/7:3500.22 create
          exit
      exit

  vprn 10101 customer 1 create
      interface X create
          description X
          address 192.168.19.130/30
          sap 1/3/7:3500.21 create
          exit
      interface Y create
          description X
          address 192.168.19.130/30
          sap 1/3/7:3500.21 create
          exit
      exit

Template:

 <group name="{{ service_type }}">
      {{ service_type }} {{id}} customer {{id2}} create
      <group name="interfaces*">
          interface {{interface_name}} create
              address {{address}}
              sap {{sap_port}} create
          exit {{_end_}}
       </group>
       exit {{_end_}}
</group>

Result:

[
    {
        "ies": {
            "id": "10102",
            "id2": "1",
            "interfaces": [
                {
                    "address": "192.168.19.134/30",
                    "interface_name": "\"Y\"",
                    "sap_port": "1/3/7:3500.22"
                }
            ]
        },
        "vprn": {
            "id": "10101",
            "id2": "1",
            "interfaces": [
                {
                    "address": "192.168.19.130/30",
                    "interface_name": "X",
                    "sap_port": "1/3/7:3500.21"
                },
                {
                    "address": "192.168.19.130/30",
                    "interface_name": "Y",
                    "sap_port": "1/3/7:3500.21"
                }
            ]
        }
    }
]

You can quickly test above template here - http://textfsm.nornir.tech/