用python的nornir、netmiko模块写一个网络自动化变更脚本,包含配置前数据校验、命令合法性校验,配置过程中的报错提示,以及发生错误进行配置回滚,以及变更完成后的验证
时间: 2024-02-27 11:59:05 浏览: 112
以下是一个使用 Python 的 Nornir 和 Netmiko 模块编写的网络自动化变更脚本示例,包含了配置前数据校验、命令合法性校验、配置过程中的报错提示、错误回滚和变更完成后的验证功能。
```python
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_config, netmiko_send_command
from nornir.plugins.functions.text import print_result
from nornir.core.exceptions import NornirExecutionError
from netmiko import NetMikoAuthenticationException, NetMikoTimeoutException
# 配置信息
nr = InitNornir(config_file="config.yaml")
hostname = '192.0.2.1'
username = 'admin'
password = 'admin'
config_commands = [
'interface loopback 0',
'ip address 10.0.0.1 255.255.255.255',
'description Test Loopback'
]
# 数据校验
def validate_data():
# TODO: 实现你的数据校验逻辑
return True
# 命令合法性校验
def validate_commands():
# TODO: 实现你的命令合法性校验逻辑
return True
# 配置回滚
def rollback_config(task):
# TODO: 实现你的配置回滚逻辑
pass
# 变更完成后的验证
def validate_config(task):
# TODO: 实现你的变更完成后的验证逻辑
pass
# 主函数
def main():
if not validate_data():
print('Data validation failed')
return
if not validate_commands():
print('Command validation failed')
return
try:
# 备份配置
backup_config = nr.run(task=netmiko_send_command, command_string='show running-config')
# 执行配置命令
results = nr.run(task=netmiko_send_config, config_commands=config_commands)
# 检查是否有错误
failed_hosts = []
for host, result in results.items():
if isinstance(result.exception, (NetMikoAuthenticationException, NetMikoTimeoutException)):
print(f"ERROR: {host} {result.exception}")
failed_hosts.append(host)
elif isinstance(result.exception, NornirExecutionError):
print(f"ERROR: {host} {result.exception.result}")
failed_hosts.append(host)
# 如果有错误,进行回滚
if failed_hosts:
print('Configuration failed')
nr.run(task=rollback_config)
return
# 验证配置
nr.run(task=validate_config)
except Exception as e:
print('Error occurred during configuration: %s' % str(e))
nr.run(task=rollback_config)
return
print('Configuration succeeded')
if __name__ == '__main__':
main()
```
上述代码中,`validate_data` 和 `validate_commands` 函数分别用于数据校验和命令合法性校验。如果校验失败,则直接返回。
在主函数中,首先进行数据校验和命令合法性校验。如果校验失败,则直接返回。接着,使用 Nornir 和 Netmiko 模块进行 SSH 连接,并备份当前配置。接着,执行配置命令,并检查是否有错误。如果有错误,则进行错误处理和回滚。如果执行成功,则进行配置验证。最后输出结果。
需要注意的是,这个示例只是一个简单的示例,实际情况可能更加复杂。你需要根据自己的实际情况进行修改和扩展,并进行充分的测试和验证。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)