networking - What is the "Python" way to parse through mroutes -


i network engineer trying script out specific "mroute" (multicast route) exported data. trying figure out "pythonic" path this.

the data looks (nothing specific network, lab exports):

 (*,224.0.0.0/4) rpf nbr: 96.34.35.36 flags: c rpf p   up: 1w5d  (*,224.0.0.0/24) flags: d p   up: 1w5d  (*,224.0.1.39) flags: s p   up: 1w5d  (96.34.246.55,224.0.1.39) rpf nbr: 96.34.35.36 flags: rpf   up: 1w4d   incoming interface list     bundle-ether434 flags: f a, up: 1w4d   outgoing interface list     bvi100 flags: f, up: 1w4d     tengige0/0/0/3 flags: f, up: 1w4d     tengige0/0/1/1 flags: f, up: 1w4d     tengige0/0/1/2 flags: f, up: 1w4d     tengige0/0/1/3 flags: f, up: 1w4d     tengige0/1/1/1 flags: f, up: 1w4d     tengige0/1/1/2 flags: f, up: 1w4d     tengige0/2/1/0 flags: f, up: 1w4d     tengige0/2/1/1 flags: f, up: 1w4d     tengige0/2/1/2 flags: f, up: 1w4d     bundle-ether234 (0/3/cpu0) flags: f, up: 2d17h     bundle-ether434 flags: f a, up: 1w4d  (*,224.0.1.40) flags: s p   up: 1w5d   outgoing interface list     tengige0/2/1/0 flags: ii, up: 1w5d 

i have tried replicate c style loops move index incrementer when regex lines.

the end result want show multicast group if has specific output in "outgoing" section.

a horrible example of have tried far (not complete, data handed off in list):

myarray = [] myarray = output.split("\n")  max_count = len(myarray) i= 0 while (i < max_count):     if (re.match(r"(^\()", myarray[i])):         group = myarray[i]         print group         i+=1         while (re.match(r'(?!^\()', myarray[i])):             if (re.match(r"  outgoing interface list", myarray[i])):                 outgoing = myarray[i]                 print outgoing                 i+=1                 while (re.match(r'(?!^\()', myarray[i])):                     print myarray[i]                     i+=1             else:                 i+=1     else:         i+=1 

thanks advice.

using loop eliminates having use variable counter, since returns sequence number while looping.

there's simpler or better way still available also, here's 1 way thought of hope same results.

myarray = output.split("\n")  in range(len(myarray)):     if re.match('(^\()', myarray[i]):         group = myarray[i]         print group         if (re.match('(?!^\()', myarray[i])):             if re.match('\s+outgoing interface list', myarray[i]):                 outgoing = myarray[i]                 print outgoing                 if re.match('(?!^\()', myarray[i]):                     print myarray[i] 

my results were:

(*,224.0.0.0/4) (*,224.0.0.0/24) (*,224.0.1.39) (96.34.246.55,224.0.1.39) (0/3/cpu0) (*,224.0.1.40) 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -