-1

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
Tharaka Devinda
  • 1,952
  • 21
  • 23
BaoJia
  • 3
  • 3
  • 1
    In `somelist.remove("Interface Vlan*")` the `*` doesn't work as a wildcard character. You can use regex to do that or `for i in somelist: if "Interface Vlan" in i: ...` – game0ver Aug 29 '18 at 11:19

1 Answers1

0

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.

Connolly
  • 76
  • 4
  • So (item) will be any match of interface vlan. It seems like I was close to it . I’ll try as soon as possible. –  BaoJia Aug 30 '18 at 00:44
  • I got it to work with for item in NoDot1x: if "Interface Vlan": NoDot1x.remove(item) –  BaoJia Aug 30 '18 at 12:19