Python 通过Cisco配置解析

Python 通过Cisco配置解析,python,cisco,configparser,Python,Cisco,Configparser,我一直在尝试编写一个Python脚本来检查20多个cisco配置 到目前为止,我得到了一个脚本来处理一个配置,它解析出了没有802.1x身份验证*接口命令的接口 从列表中删除接口Tengig和接口vlan*时遇到问题。最好的方法是什么?我尝试了下面的方法,但没有成功。也许我只是用错了 list.remove(Interface Vlan*) for item in list(NoDot1x): somelist.remove("Interface Vlan*") 代码: 这里是#显示结

我一直在尝试编写一个Python脚本来检查20多个cisco配置

到目前为止,我得到了一个脚本来处理一个配置,它解析出了没有
802.1x身份验证*
接口命令的接口

从列表中删除
接口Tengig
接口vlan*
时遇到问题。最好的方法是什么?我尝试了下面的方法,但没有成功。也许我只是用错了

list.remove(Interface Vlan*) 

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

这里是#显示结果的输出

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

Python中list对象提供的remove函数将尝试查找与输入的字符串完全匹配的字符串,如果找到则删除,否则将出错

#Remove Uplinks
for item in list(NoDot1x):
 somelist.remove("Interface Vlan*")
上述内容不会使用通配符“*”。 我想你想要的东西更像:

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

如果您需要更高的复杂性,请查看重新导入。

somelist.remove(“接口Vlan*”)
中,
*
不能作为通配符使用。您可以使用regex来实现这一点,或者对somelist中的i使用
:如果i:…
中的“接口Vlan”,那么(项)将是接口Vlan的任何匹配项。看来我离它很近了。我将尽快尝试。我已将其用于NoDot1x:if“Interface Vlan”:NoDot1x.remove(项目)中的项目
for item in list(NoDot1x):
 if "Interface Vlan" in item:
  somelist.remove(item)