之前:
hostname Foo
!
interface GigabitEthernet 1/1
switchport mode trunk
switchport trunk allowed vlan 10,20,30
!
interface GigabitEthernet 1/2
ip address 192.0.2.1 255.255.255.128
no ip proxy-arp
!
之后:
hostname Foo
!
interface GigabitEthernet 1/1
switchport mode trunk
switchport allowed vlan 10,20,30,40
!
interface GigabitEthernet 1/2
ip address 192.0.2.1 255.255.255.128
!
我知道我可以使用
difflib
来区分这两个字符串,但是 difflib 不理解 Cisco IOS 语法。
Difflib 示例
import difflib
before = open('before.txt').readlines()
after = open('after.txt').readlines()
# Find and print the diff:
for line in difflib.unified_diff(
before, after, fromfile='before.txt',
tofile='after.txt', lineterm=''):
print(line)
当我运行此命令时,我得到:
--- before.txt
+++ after.txt
@@ -2,9 +2,8 @@
!
interface GigabitEthernet 1/1
switchport mode trunk
- switchport trunk allowed vlan 10,20,30
+ switchport allowed vlan 10,20,30,40
!
interface GigabitEthernet 1/2
ip address 192.0.2.1 255.255.255.128
- no ip proxy-arp
!
这不是适用于 Cisco IOS 的差异。 最重要的是,
interface
名称不包含在接口命令的差异集中(例如
no ip proxy-arp
);但是,正确的 Cisco IOS 差异需要interface 命令。
所需的输出
使用 python,如何获得与此类似的差异输出,它理解 Cisco 配置格式?
interface GigabitEthernet 1/1
switchport allowed vlan 10,20,30,40
interface GigabitEthernet 1/2
ip proxy-arp
注意每个接口命令如何diff 前面是接口名称;此外,由于
ip proxy-arp
已从原始配置中删除,因此添加了
no ip proxy-arp
命令。
可以使用 Cisco 的 python 库来解析配置并生成差异。以下是一个如何执行此操作的示例:
from ciscoconfparse import CiscoConfParse
# 从文件中加载旧配置和新配置
with open("before.txt") as f:
before_config = f.read()
with open("after.txt") as f:
after_config = f.read()
# 使用 CiscoConfParse 解析配置
before_parse = CiscoConfParse(before_config.splitlines())
after_parse = CiscoConfParse(after_config.splitlines())
# 查找已添加或已删除的父行
before_parent_lines = set(cfg_ln.text for cfg_ln in before_parse.find_objects(r"^interface"))
after_parent_lines = set(cfg_ln.text for cfg_ln in after_parse.find_objects(r"^interface"))
added_parents = after_parent_lines - before_parent_lines
removed_parents = before_parent_lines - after_parent_lines
# 打印已添加或已删除的父行
print("以下接口已添加:")
for parent in added_parents:
print(parent)
print("以下接口已删除:")
for parent in removed_parents:
print(parent)
# 查找已添加或已删除的子行
for parent in before_parent_lines.intersection(after_parent_lines):
before_children = before_parse.find_objects(f"^{parent}")
after_children = after_parse.find_objects(f"^{parent}")
before_children_text = set(child.text for child in before_children)
after_children_text = set(child.text for child in after_children)
added_children = after_children_text - before_children_text
removed_children = before_children_text - after_children_text
# 打印已添加或已删除的子行
if added_children or removed_children:
print(f"接口 {parent} 有以下更改:")
for child in added_children:
print(f"+ {child}")
for child in removed_children:
print(f"- {child}")
此脚本首先加载旧配置和新配置,并使用
CiscoConfParse
解析它们。然后,它会查找已添加或已删除的父行(在本例中为接口行)。对于每个父行,它会查找已添加或已删除的子行。最后,它以与请求的格式类似的格式打印已添加和已删除的行的列表。
此脚本将生成以下输出:
以下接口有以下更改:
interface GigabitEthernet 1/1
+ switchport allowed vlan 10,20,30,40
- switchport trunk allowed vlan 10,20,30
interface GigabitEthernet 1/2
- no ip proxy-arp
如所见,此输出更容易理解,并且提供了有关两个 Cisco IOS 配置之间差异的更多信息。
标签:python,cisco-ios From: 78759473