get content in between a matched pattern and next matched pattern using python

58 Views Asked by At

I am trying to write a python script to export text formated cisco firewall configuration file to excel for easy query and filtering. I am able to write acls and each fields into excel sheet tab. however for network objects I have a little trouble.

here is an example content i have:

object-group network external_mail_client
 network-object x.x.x.x x.x.x.x
 network-object y.y.y.y y.y.y.y
 network-object z.z.z.z z.z.z.z
object-group network domain_controllers
 network-object host a.a.a.a
 network-object host b.b.b.b

here i have two object groups, and each of them have a few hosts. I am trying to write each object group and its members into the same excel row column by column. which at the end should be like this:

object-group network external_mail_client x.x.x.x x.x.x.x y.y.y.y y.y.y.y z.z.z.z z.z.z.z 
object-group network domain_controllers a.a.a.a b.b.b.b

My trouble here is how can i find all members below the object-group without missing or mis-adding other object-group's members. I am guessing I should add some kind of line count and number check and etc. but could not figure out a better solution.

Please help. thanks.

1

There are 1 best solutions below

0
On

You can have your script execute:

show run | i object-group network
(output)
object-group network external_mail_client
object-group network domain_controllers

Then loop through these with:

show run object-group id external_mail_client
show run object-group id domain_controllers

That will return only the members of that specific object-group.