我正在尝试使用 python 将 cisco 运行配置转换为参数,但我一直坚持使用 python 读取配置部分。例如,假设您有以下节:!interface Async1 no ip address encapsulation slip! router bgp 65500 bgp router-id 1.1.1.1 bgp log-neighbor-changes timers bgp 10 30 neighbor 1.2.3.4 remote-as 1234 neighbor 1.2.3.4 description Some Description neighbor 1.2.3.4 update-source GigabitEthernet0/0 ! address-family ipv4 network 2.2.2.2 mask 255.255.255.255 network 3.3.3.0 mask 255.255.255.252 neighbor 1.2.3.4 activate neighbor 1.2.3.4 allowas-in 3 neighbor 1.2.3.4 prefix-list PXL out exit-address-family!ip forward-protocol ndno ip http serverno ip http secure-server!我想从“路由器 bgp”中读取行,直到第一行以“!”开头 (例如^!),然后重新读取块以将参数提取到变量中。示例输出将是:router bgp 65500 bgp router-id 1.1.1.1 bgp log-neighbor-changes timers bgp 10 30 neighbor 1.2.3.4 remote-as 1234 neighbor 1.2.3.4 description Some Description neighbor 1.2.3.4 update-source GigabitEthernet0/0 ! address-family ipv4 network 2.2.2.2 mask 255.255.255.255 network 3.3.3.0 mask 255.255.255.252 neighbor 1.2.3.4 activate neighbor 1.2.3.4 allowas-in 3 neighbor 1.2.3.4 prefix-list PXL out exit-address-family!注意:我可以使用 awk 或 grep 提取上述代码,但我想将我的 bash 代码转换为 Python。谢谢!
2 回答
森栏
TA贡献1810条经验 获得超5个赞
试试这个:
from ciscoconfparse import CiscoConfParse
bgp = confparse.find_blocks(r"^router bgp 65500")
添加回答
举报
0/150
提交
取消