[python]Matching sh run object service with textfsm

I have been experimenting a few combination with textfsm, the problematic one is show run object service may show description and some may not show description this is because description is an optional configuration item in Cisco ASA. This makes matching description kind of hard.

The current working textfsm only matches compulsory items such as the tcp port numbers in range and in single port and the object name.
The problem with the below template is it cannot match for output that has description, hence the template below only captures everything except for description.

Value svc_name (\w+)
Value protocol (tcp|udp)
Value start_port ([0-9A-Za-z]{1,5})
Value end_port ([0-9A-Za-z]{1,5})
Value type (eq|range)
#Value desc ([\w+\s*]+)

Start
 ^object\s+service\s+${svc_name} -> ServiceType

ServiceType
 ^\s+service\s+${protocol}\s+destination\s+${type}\s+${start_port}\s*${end_port}? -> Record Start

From the textfsm documentation, you can route the match to another state, in this textfsm template the state is ServiceType. There are two line actions one is Continue the other is Next, Next is the default if no action is specified, and there are four record actions: NoRecord (default if no record action is specified), Record, Clear and Clearall.

To prevent the parsing from looping infinitely, Continue line action can change state, hence I cannot use Continue ServiceType which raises exception.

Improve on the template to capture optional description

With reference to this template for matching show run object network, a working template for matching show run object service is developed.

Value Required svc_name (\w+)
Value protocol (tcp|udp)
Value start_port ([0-9A-Za-z]{1,5})
Value end_port ([0-9A-Za-z]{1,5})
Value type (eq|range)
Value desc ([\w+\s*]+)

Start
 ^object service -> Continue.Record
 ^object service ${svc_name}
 ^\s+description\s+${desc}\s*
 ^\s+service\s+${protocol}\s+destination\s+${type} -> Continue
 ^\s+service\s+${protocol}\s+destination\s+range\s+${start_port}\s+${end_port}\s*
 ^\s+service\s+${protocol}\s+destination\s+eq\s+${start_port}\s*
 ^. -> Error

I tested the improved template, the output matches for both the range and eq with and without description.

The Required keyword for svc_name, will make the parsing record if the match of svc_name is found.

Continue.Record, the continue does not match the first row of the state, in this case is the Start state, then try the next row if a match found it is recorded.

^\s+service\s+${protocol}\s+destination\s+${type} -> Continue This line is interesting, it actually “ignores” this row, and continue to the next row to match the exact and record.

The last row is ^. -> Error is a catch all rule, if none is match the record will be discarded.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s