Cisco Config Parser multiple lines

204 views Asked by At

I am working on a parse for verifying configs for wlan controllers and I thought I had something. I usually use show commands but all I have available are the configs in a text format.

I have some configs that have either or of these lines. They are pulled and saved with putty.

VLAN........................ 
..................... 900      



VLAN............................................. 900 

Code I am using is rather simple

from ciscoconfparse import CiscoConfParse

parse = CiscoConfParse('config.txt', syntax='ios')

vlan = parse.find_objects(r'^VLAN')
#print(vlan)
vlan_id = vlan[0]
print(" VLAN: {0}".format(vlan_id.text))

if in the config it is one line I can trim it up and get the vlan id, but if it is in two lines I do not get the vlan id.

With this in the config I do not get a vlan id

VLAN........................ 
..................... 900    

This I do

VLAN............................................. 900 

I am looking for a way just to get the vlan id on either way it is formatted in the conig.

Thank you

1

There are 1 answers

0
Mike Pennington On

In this case, you don't need CiscoConfParse()... just use split()

This would work...

 $ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input1 = "VLAN............................................. 900"
>>> input2 = """VLAN........................
... ..................... 900"""
>>>
>>> input1.split()[-1]
'900'
>>>
>>> input2.split()[-1]
'900'
>>>