pythonciscociscoconfparse

Parse through Cisco Config


I been trying to write a Python script to go through 20 some cisco configurations.

So Far, I got a script to work on one config and it parsed out the interfaces that do not have 802.1x authentication* interface commands.

I'm having trouble removing the interface Tengig and interface vlan* from the list. What is the best way to do that? I tried the following ways below with no luck. Maybe I'm just using them wrong?

list.remove(Interface Vlan*) 

for item in list(NoDot1x):
  somelist.remove("Interface Vlan*")

Code:

    #Int Modules
    from ciscoconfparse import CiscoConfParse

    #Define what to Parse
    parse = CiscoConfParse("/home/jbowers/Configs/SwithConfig")

    #Define all Interfaces without Authentication * commands
    all_intfs = parse.find_objects(r"^interf")
    NoDot1x = list()
    NoDot1x = parse.find_objects_wo_child(r'^interface', r'authentication')

   #Display Results
   for obj in NoDot1x:
       print obj.text

   #Remove Uplinks
   for item in list(NoDot1x):
     somelist.remove("Interface Vlan*")

Here the output for #Display Results.

interface Port-channel1
interface FastEthernet1
interface GigabitEthernet1/1
interface TenGigabitEthernet5/1
interface TenGigabitEthernet5/2
interface TenGigabitEthernet5/3
interface TenGigabitEthernet5/4
interface TenGigabitEthernet5/5
interface TenGigabitEthernet5/6
interface TenGigabitEthernet5/7
interface TenGigabitEthernet5/8
interface TenGigabitEthernet6/1
interface TenGigabitEthernet6/2
interface TenGigabitEthernet6/3
interface TenGigabitEthernet6/4
interface TenGigabitEthernet6/5
interface TenGigabitEthernet6/6
interface TenGigabitEthernet6/7
interface TenGigabitEthernet6/8
interface GigabitEthernet7/23
interface GigabitEthernet8/17
interface GigabitEthernet9/2
interface Vlan1

Solution

  • The remove function offered by the list object in Python will attempt to find an exact match to the string entered and remove if found, if not it will error.

    #Remove Uplinks
    for item in list(NoDot1x):
     somelist.remove("Interface Vlan*")
    

    The above will not utilize the wildcard character "*". I think you want something more like:

    for item in list(NoDot1x):
     if "Interface Vlan" in item:
      somelist.remove(item)
    

    If you need more complexity look at the re import.