Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python to find Cisco IOS interfaces with a specific config

I have hundreds of cisco configuration files and I need to find (via python) the interfaces with a specific service policy applied in this case WIRELESS-IN.

This is the regex I used to capture the interface:

pat=re.compile('(interface.*?)!$',re.DOTALL|re.M)

That should return "FastEthernet1/0/2".

My regex classifies interface elements into a list of lines, but how should I search for WIRELESS-IN within the list of lines?

Example configuration:

interface FastEthernet1/0/1
 description Foo
 switchport access vlan 300
 switchport mode access
 switchport port-security aging time 2
 no logging event link-status
 speed 100
 duplex full
 priority-queue out
 mls qos trust dscp
 no snmp trap link-status
 no cdp enable
 spanning-tree portfast
 hold-queue 120 in
 hold-queue 200 out
 ip dhcp snooping trust
!
interface FastEthernet1/0/2
 description wlap2
 switchport access vlan 100
 switchport mode access
 switchport port-security maximum 15
 switchport port-security
 switchport port-security aging time 2
 switchport port-security aging type inactivity
 ip access-group 100 in
 no logging event link-status
 srr-queue bandwidth shape 0 0 0 10
 queue-set 2
 priority-queue out
 no snmp trap link-status
 storm-control broadcast level pps 100 50
 storm-control multicast level pps 100 50
 storm-control action trap
 spanning-tree portfast
 spanning-tree bpduguard enable
 service-policy input WIRELESS_IN
 ip dhcp snooping limit rate 15
!
interface FastEthernet1/0/3
 description Test3
 switchport access vlan 199
 switchport mode access
 switchport port-security aging time 2
 no logging event link-status
 queue-set 2
 priority-queue out
 mls qos trust dscp
 no snmp trap link-status
 no cdp enable
 spanning-tree portfast
 service-policy input VOICE-LAN

This is what I tried using:

import re,string
f = open("sampleconfig.cfg")
cfgdata = f.read()
pat=re.compile('(interface.*?)!$',re.DOTALL|re.M)
pat2 = re.compile("service-policy.input.WIRELESS-IN")

data = pat.findall(cfgdata)
i=0
while i < len(data):
    if  pat2.findall(data[i]):
        print (data[i].split("\n")[0])
        i = i+1

    else:
        i = i+1
        pass
like image 902
Deep6 Avatar asked May 03 '26 02:05

Deep6


1 Answers

I have hundreds of cisco configuration files and I need to find (via python) the interfaces with a specific service policy applied in this case WIRELESS-IN. The Regex I am using to capture the interface is: pat=re.compile('(interface.*?)!$',re.DOTALL|re.M) ...

I'd need to return FastEthernet1/0/2 in this case.

Answer:

You should use ciscoconfparse1... assume I stored your configuration in a file called stackoverflow_question.conf...

>>> from ciscoconfparse import CiscoConfParse
>>> parse = CiscoConfParse('stackoverflow_question.conf')
>>> # .find_parents_w_child() returns a list of matching parent conf lines
>>> wifi_qos_intfs = parse.find_parents_w_child('^interface', 'WIRELESS_IN')
>>> print(wifi_qos_intfs)
['interface FastEthernet1/0/2']
>>>

How it works

ciscoconfparse's API is fairly simple; it breaks the Cisco configuration into parents / children. service-policy input WIRELESS_IN is considered a child of interface FastEthernet1/0/2.

When you call CiscoConfParse(configuration_file), ciscoconfparse builds relationships to Cisco IOS config lines as parent and child statements; an object with all those relationships is returned. Then parse.find_parents_w_child(parentstr, childstr), is a method on the aforementioned object, which walks the configuration and returns a list of text lines which match parentstr, and have one or more children matching childstr.

I used a regex like ^interface to guarantee that parentstr only matches Cisco IOS interface lines. and not other statements that might also use the word interface in them.


1Full-disclosure: I am ciscoconfparse's author.

like image 89
Mike Pennington Avatar answered May 05 '26 15:05

Mike Pennington