mpenning / ciscoconfparse

Parse, Audit, Query, Build, and Modify Arista / Cisco / Juniper / Palo Alto / F5 configurations.
http://www.pennington.net/py/ciscoconfparse/
GNU General Public License v3.0
783 stars 222 forks source link

Incorrect results on find_children & find_all_children #8

Closed rishikeshr closed 9 years ago

rishikeshr commented 9 years ago

Hi, I am parsing a config file and I can see that the results are varying and not consistent.

To begin with, my test config file is

interface VlanAlpha3

interface VlanAlpha2

interface VlanAlpha1

interface VlanAlpha
  vrf member alpha01
  some config1 
  ip address 100.100.102.0/24
  some config2
  blah blah blah

interface VlanAlpha32

interface VlanAlpha22

interface VlanAlpha12

interface VlanAlpha2
  vrf member alpha012
  some config1 
  ip address 100.100.101.0/24
  some config2
  blah blah blah

interface VlanAlpha31

interface VlanAlpha21

interface VlanAlpha11

interface VlanAlpha1
  vrf member alpha011
  no shutdown
  ip flow monitor netflow-monitor input  
    no ip redirects
    ip address 100.100.100.0/24
    ip ospf passive-interface
    ip router ospf 1 area 0.0.0.3
  ip pim sparse-mode
  hsrp version 2
  hsrp 224 

With the following code, I get an output, which seems incorrect.

from ciscoconfparse import CiscoConfParse
parseObject = CiscoConfParse("testconfig1")
allChildren = parseObject.find_all_children('interface VlanAlpha1')
print allChildren
 ['interface VlanAlpha1', 'interface VlanAlpha12', 'interface VlanAlpha11', 'interface ?VlanAlpha1', '  vrf member alpha011']

Shouldnt this list all the child nodes of Interface VlanAlpha1 I am using Python v 2.7.5 and CiscoConfParse v 1.1.1

mpenning commented 9 years ago

Thank you for asking. This is not a bug, in fact it is covered both in the API documentation and the FAQ.

If you only need to match against "interface VlanAlpha1" and its children, call find_all_children(r"interface VlanAlpha1", exactmatch=True)

rishikeshr commented 9 years ago

Hello Mike,

Appreciate your prompt response on the issue I am facing. I tried exact match as well, but I dont see all the child nodes being listed.

allChildren = parseObject.find_all_children('interface VlanAlpha1',exactmatch=True)
print allChildren
['interface VlanAlpha1', 'interface VlanAlpha1', '  vrf member alpha011']
mpenning commented 9 years ago

CiscoConfParse requires exclaimation points (!) instead of blank lines... this script seems to work correctly...

(py27_test)[mpenning@tsunami ~]$ cat github_8.py 
from ciscoconfparse import CiscoConfParse

config = """interface VlanAlpha3
!
interface VlanAlpha2
!
interface VlanAlpha1
!
interface VlanAlpha
  vrf member alpha01
  some config1 
  ip address 100.100.102.0/24
  some config2
  blah blah blah
!
interface VlanAlpha32
!
interface VlanAlpha22
!
interface VlanAlpha12
!
interface VlanAlpha2
  vrf member alpha012
  some config1 
  ip address 100.100.101.0/24
  some config2
  blah blah blah
!
interface VlanAlpha31
!
interface VlanAlpha21
!
interface VlanAlpha11
!
interface VlanAlpha1
  vrf member alpha011
  no shutdown
  ip flow monitor netflow-monitor input  
    no ip redirects
    ip address 100.100.100.0/24
    ip ospf passive-interface
    ip router ospf 1 area 0.0.0.3
  ip pim sparse-mode
  hsrp version 2
  hsrp 224
"""

parse = CiscoConfParse(config.split('\n'))
allChildren = parse.find_all_children('interface VlanAlpha1',exactmatch=True)

print allChildren

When I run that...

(py27_test)[mpenning@tsunami ~]$ python github_8.py 
['interface VlanAlpha1', 'interface VlanAlpha1', '  vrf member alpha011', '  no shutdown', 
'  ip flow monitor netflow-monitor input  ', '    no ip redirects', '    ip address 100.100.100.0/24', 
'    ip ospf passive-interface', '    ip router ospf 1 area 0.0.0.3', '  ip pim sparse-mode', 
'  hsrp version 2', '  hsrp 224']
(py27_test)[mpenning@tsunami ~]$
(py27_test)[mpenning@tsunami ~]$ 
rishikeshr commented 9 years ago

Yes, that seems to work!! ! seems to be the key.

Thanks, will use that!! Again, appreciate your prompt response for the issue!!