通过 Cisco Config 解析
Parse through Cisco Config
我一直在尝试编写一个 Python 脚本来完成 20 个 cisco 配置。
到目前为止,我得到了一个脚本来处理一个配置,它解析出没有 802.1x authentication*
接口命令的接口。
我无法从列表中删除 interface Tengig
和 interface vlan*
。最好的方法是什么?我尝试了以下方法但没有成功。也许我只是用错了?
list.remove(Interface Vlan*)
for item in list(NoDot1x):
somelist.remove("Interface Vlan*")
代码:
#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*")
这里是#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
Python 中的列表对象提供的删除功能将尝试查找与输入的字符串完全匹配的内容,如果找到则删除,否则将出错。
#Remove Uplinks
for item in list(NoDot1x):
somelist.remove("Interface Vlan*")
以上不会使用通配符“*”。
我想你想要更像的东西:
for item in list(NoDot1x):
if "Interface Vlan" in item:
somelist.remove(item)
如果您需要更复杂的内容,请查看重新导入。
我一直在尝试编写一个 Python 脚本来完成 20 个 cisco 配置。
到目前为止,我得到了一个脚本来处理一个配置,它解析出没有 802.1x authentication*
接口命令的接口。
我无法从列表中删除 interface Tengig
和 interface vlan*
。最好的方法是什么?我尝试了以下方法但没有成功。也许我只是用错了?
list.remove(Interface Vlan*)
for item in list(NoDot1x):
somelist.remove("Interface Vlan*")
代码:
#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*")
这里是#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
Python 中的列表对象提供的删除功能将尝试查找与输入的字符串完全匹配的内容,如果找到则删除,否则将出错。
#Remove Uplinks
for item in list(NoDot1x):
somelist.remove("Interface Vlan*")
以上不会使用通配符“*”。 我想你想要更像的东西:
for item in list(NoDot1x):
if "Interface Vlan" in item:
somelist.remove(item)
如果您需要更复杂的内容,请查看重新导入。