Optional string in textFSM template

1.6k views Asked by At

I have a cli output by moxa switch show running-config. Interface section may or may not have a name string.

interface ethernet 1/3
 shutdown
 name Unused
 speed-duplex Auto
 no flowcontrol
 media cable-mode auto
 no gmrp
 switchport access vlan  1
 rate-limit port-disable ingress rate none
 no ptp
!

interface ethernet 1/8
 shutdown
 speed-duplex Auto
 no flowcontrol
 media cable-mode auto
 no gmrp
 switchport access vlan  1
 rate-limit port-disable ingress rate none
 no ptp

This my FSM template for parsing

Value port (\d\/\d)
Value state (shutdown|no shutdown)
Value desc (\S+)

Start
  ^interface ethernet ${port} -> Continue.Record
  ^.${state}
  ^.name.${desc}

But this way the output of the interface name is shifted down one line. How can i fix this template?

Output example

port    state        desc
------  -----------  ----------------
1/1
1/2     no shutdown  Cisco_2960_OTPSS
1/3     no shutdown  Mirror
1/8     shutdown     Unused
1/9     shutdown
        no shutdown  Proverka
1

There are 1 answers

0
n1colas.m On

The link TextFSM usage shows an example fairly similar to your problem when describing incomplete route entries:

...incomplete route entry should really be written when the next full route entry appears, but at the same time they should be written to appropriate route. The following should be done: once full route entry is met, the previous values should be written down and then continue to process the same full route entry to get its information.

In your context:

^interface ethernet -> Continue.Record

Here, Record action tells you to write down the current value of variables. Since there are no variables in this rule, what was in the previous values is written. Continue action says to continue working with the current line as if there was no match.

Start
  ^interface ethernet -> Continue.Record
  ^interface ethernet ${port}
  ^.${state}
  ^.name.${desc}
port    state     desc
------  --------  ------
1/3     shutdown  Unused
1/8     shutdown