google / textfsm

Python module for parsing semi-structured text into python tables.
Apache License 2.0
1.11k stars 171 forks source link

Question on how to parse this output #124

Open evilmonkey19 opened 3 months ago

evilmonkey19 commented 3 months ago

Hi!

I'm currently using TextFSM in NTC_Templates and so far has been an awesome experience. Thanks for the awesome project!

I would like to ask about a specific problem i came across from a CLI output. Basically I want to be able to parse the data using different templates or whatever. The thing I would love is to be able to parse this output in just one single object:

  -----------------------------------------------------------------------------
  FEC upstream switch :Disable
  OMCC Encrypt switch :On
  Qos mode            :Pq
  Mapping mode        :Vlan
  Tr069 management    :Disable
  -----------------------------------------------------------------------------
  Notes: * indicates Discrete TCONT(TCONT Unbound)
  -----------------------------------------------------------------------------
  <T-CONT   0>          DBA Profile-ID:1
  <T-CONT   1>          DBA Profile-ID:2
  <T-CONT   4>          DBA Profile-ID:5
    <Gem Index 126>
    ------------------------------------------------------------------------
    |Serv-Type:ETH |Encrypt:off |Cascade:off |GEM-CAR:-            |
    |Upstream-priority-queue:-  |Downstream-priority-queue:-       |
    ------------------------------------------------------------------------
     Mapping VLAN  Priority Port   Port  Bundle  Flow  Transparent
     index                  type   ID    ID      CAR
    ------------------------------------------------------------------------
     0       500   -        -      -     -       -     -         
    ------------------------------------------------------------------------

The output should be something as follows:

   [{
      "fec_upstream_switch": "Disable",
      "omcc_encrypt_switch": "On",
      "qos_mode": "pq",
      "mapping_mode": "vlan",
      "tr069_management": "disable",
      "t-conts": [
         {
             "t_cont_id": 0,
             "dba_profile_id": 1,
         },
         {
             "t_cont_id": 1,
             "dba_profile_id": 2,
         },
         {
              "t_cont_id":4,
              "dba_profile_id":5,
          }
      ],
      "gems": [
         {
            "gem_index": 126,
            "t_cont_id": 4,
            "serv-type": "eth",
            "encrypt": "off",
            "gem_car": "",
            "upstream_priority_queue": "",
            "downstream_priority_queue": "",
         } 
     "gem_mappings": [
        {
           "gem_index": 126,
          "mapping_index": 0,
          "vlan": 500,
          "priority": "",
           "port_type": "",
            "bundle_id": "",
           "flow_car": "",
           "transparent": "",
        }
      ]
   }]

I accept any other suggestion as well! I suggest to reuse the id and index as a way to connect each part, but if it can nested inside even better instead of relating using the indexes. Thanks!

mjbear commented 2 months ago

To close the loop for the broader community, there is a thread over at ntc-templates.

The summary of that thread being TextFSM is not set up for providing (deeply?) nested data. If nested data is required, maybe modify it after textfsm parsing (post-parse) or there might be another project that does this out-of-the-box. There's an awesome network automation list that might help show possibilities.