概要
Napalm(Network Automation and Programmability Abstraction Layer with Multivendor support)是一个Python库,旨在提供统一的API接口来与不同厂商的网络设备进行交互。在网络工程师和自动化工程师面临的最大挑战之一是不同网络设备厂商(如Cisco、Juniper、Arista等)使用不同的操作系统和命令行接口,这使得编写跨平台的网络自动化脚本变得困难。Napalm通过提供抽象层,让工程师能够使用一致的方法来配置和管理来自不同厂商的网络设备,大大简化了网络自动化的复杂性。
配置环境
Napalm的安装非常简单,可以通过pip直接安装基本包:
# 安装基本的Napalm包
pip install napalm
如果只需要支持特定厂商的设备,可以安装相应的驱动:
# 安装特定厂商驱动(例如,仅Cisco IOS)
pip install napalm-ios
# 或安装多个厂商驱动
pip install napalm-ios napalm-junos napalm-eos
安装完成后,可以通过以下简单代码验证安装是否成功:
# 验证Napalm安装
import napalm
import json
# 打印支持的驱动列表
print("支持的设备驱动:")
print(napalm.get_network_driver_names())
# 获取特定驱动(例如IOS)
ios_driver = napalm.get_network_driver('ios')
print(f"已成功加载 {ios_driver.__name__} 驱动")
如果能够成功导入napalm并列出支持的驱动,则说明安装成功。Napalm支持多种常见的网络设备操作系统,包括Cisco IOS/IOS-XR/NX-OS、Juniper JunOS、Arista EOS等。
主要特性
厂商无关的统一API:使用相同的方法操作不同厂商的设备
配置管理:加载、比较和合并配置
状态检索:获取设备各种操作状态和统计信息
交易式配置:支持配置回滚和验证
连接方法灵活:支持SSH、API等多种连接方式
设备发现:自动检测网络拓扑和设备信息
与其他框架集成:可与Ansible、SaltStack等工具无缝配合
强大的验证功能:验证网络状态是否符合预期
支持YANG模型:通过napalm-yang扩展支持基于模型的配置
开源社区支持:活跃的开发者社区和持续改进
基本功能
1、连接设备并检索基本信息
Napalm最基本的功能是连接网络设备并获取信息。
以下代码展示了如何使用Napalm连接设备并获取基本系统信息,这是网络自动化中的第一步,能够帮助工程师了解设备的基本状态和配置。
from napalm import get_network_driver
import json
# 选择设备类型(这里以Cisco IOS为例)
driver = get_network_driver('ios')
# 设备连接参数
device = driver(
hostname='192.168.1.1',
username='admin',
password='password',
optional_args={'secret': 'enable_password'} # 如果需要enable密码
)
# 打开连接
device.open()
try:
# 获取基本设备信息
facts = device.get_facts()
# 格式化输出
print(json.dumps(facts, indent=4))
# 输出主要信息
print(f"设备型号: {facts['model']}")
print(f"序列号: {facts['serial_number']}")
print(f"操作系统: {facts['os_version']}")
print(f"主机名: {facts['hostname']}")
print(f"接口数量: {len(facts['interface_list'])}")
finally:
# 关闭连接
device.close()
2、配置管理与比较
Napalm的核心优势之一是其配置管理能力,特别是它能够比较和合并配置。
以下示例展示了如何加载、比较和部署配置,这对于网络变更管理和自动化配置部署非常有用。
from napalm import get_network_driver
# 初始化驱动
driver = get_network_driver('ios')
device = driver(
hostname='192.168.1.1',
username='admin',
password='password'
)
# 打开连接
device.open()
try:
# 准备新配置(可以从文件加载或直接使用字符串)
new_config = """
interface GigabitEthernet0/1
description CONNECTED TO SWITCH
ip address 10.0.0.1 255.255.255.0
no shutdown
!
ip route 192.168.100.0 255.255.255.0 10.0.0.2
"""
# 加载候选配置但不提交(合并模式)
device.load_merge_candidate(config=new_config)
# 获取差异
diff = device.compare_config()
if diff:
print("配置差异:")
print(diff)
# 确认后提交配置
user_input = input("是否应用这些更改? (yes/no): ")
if user_input.lower() == 'yes':
device.commit_config()
print("配置已提交")
else:
device.discard_config()
print("配置已丢弃")
else:
print("没有配置差异")
finally:
# 关闭连接
device.close()
3、获取设备操作状态
网络设备的状态监控是网络管理中的关键任务。Napalm提供了多种获取设备运行状态的方法,以下示例展示了如何获取接口信息和路由表,这对于故障排除和网络可视化非常实用。
from napalm import get_network_driver
import json
# 初始化驱动
driver = get_network_driver('ios')
device = driver(
hostname='192.168.1.1',
username='admin',
password='password'
)
# 打开连接
device.open()
try:
# 获取接口详情
interfaces = device.get_interfaces()
interfaces_ip = device.get_interfaces_ip()
# 获取路由表
routes = device.get_route_to('8.8.8.8')
# 获取ARP表
arp_table = device.get_arp_table()
# 获取NTP信息
ntp_stats = device.get_ntp_stats()
# 获取BGP邻居
bgp_neighbors = device.get_bgp_neighbors()
# 打印接口状态
print("接口状态:")
for interface, details in interfaces.items():
status = "UP"if details["is_up"] else"DOWN"
print(f"{interface}: {status}, MAC: {details['mac_address']}, MTU: {details['mtu']}")
# 打印IP地址信息
print("
接口IP地址:")
for interface, ip_details in interfaces_ip.items():
if'ipv4'in ip_details:
for ip, prefix in ip_details['ipv4'].items():
print(f"{interface}: {ip}/{prefix['prefix_length']}")
# 打印到8.8.8.8的路由
print("
到8.8.8.8的路由:")
print(json.dumps(routes, indent=2))
# 打印ARP表条目数
print(f"
ARP表条目数: {len(arp_table)}")
finally:
# 关闭连接
device.close()
高级功能
1、多设备并行操作
在大型网络环境中,需要同时操作多台设备是常见需求。以下示例展示了如何使用并行处理来同时管理多台设备,这能显著提高批量操作的效率。
from napalm import get_network_driver
import concurrent.futures
import json
# 设备列表
devices = [
{'hostname': '192.168.1.1', 'device_type': 'ios', 'username': 'admin', 'password': 'password'},
{'hostname': '192.168.1.2', 'device_type': 'eos', 'username': 'admin', 'password': 'password'},
{'hostname': '192.168.1.3', 'device_type': 'junos', 'username': 'admin', 'password': 'password'}
]
# 处理单个设备的函数
def process_device(device_info):
driver = get_network_driver(device_info['device_type'])
device = driver(
hostname=device_info['hostname'],
username=device_info['username'],
password=device_info['password']
)
results = {}
try:
device.open()
# 获取设备信息
results['facts'] = device.get_facts()
# 获取接口状态
results['interfaces'] = device.get_interfaces()
device.close()
return {device_info['hostname']: results}
except Exception as e:
return {device_info['hostname']: f"错误: {str(e)}"}
# 使用线程池并行处理所有设备
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# 提交所有任务
future_to_device = {executor.submit(process_device, device): device for device in devices}
# 收集结果
all_results = {}
for future in concurrent.futures.as_completed(future_to_device):
device = future_to_device[future]
try:
data = future.result()
all_results.update(data)
except Exception as e:
all_results[device['hostname']] = f"处理异常: {str(e)}"
# 打印结果摘要
for hostname, results in all_results.items():
if isinstance(results, dict) and'facts'in results:
print(f"{hostname}: {results['facts']['model']}, 接口数量: {len(results['interfaces'])}")
else:
print(f"{hostname}: {results}")
2、网络合规性验证
Napalm提供了强大的验证功能,可以检查网络状态是否符合预期。以下示例展示了如何使用验证功能确保网络配置符合组织标准,这对于确保网络安全性和可靠性非常重要。
from napalm import get_network_driver
import yaml
# 验证规则(通常存储在YAML文件中)
validation_rules = """
---
- get_facts:
os_version: 15.1
- get_interfaces_ip:
GigabitEthernet0/1:
ipv4:
10.0.0.1:
prefix_length: 24
- ping:
_target: 8.8.8.8
_success:
packet_loss: 0
- get_bgp_neighbors:
global:
peers:
192.168.0.2:
is_enabled: true
is_up: true
"""
# 将YAML字符串转换为Python对象
rules = yaml.safe_load(validation_rules)
# 初始化驱动
driver = get_network_driver('ios')
device = driver(
hostname='192.168.1.1',
username='admin',
password='password'
)
# 打开连接
device.open()
try:
# 执行验证
result = device.compliance_report(rules)
# 打印验证结果
print(f"验证通过: {result['complies']}")
ifnot result['complies']:
print("
不符合规则的项目:")
for rule, details in result.items():
if rule != 'complies'andnot details['complies']:
print(f"- {rule}: {details.get('reason', '不符合规则')}")
# 打印完整报告
print("
详细验证报告:")
print(yaml.dump(result, default_flow_style=False))
finally:
# 关闭连接
device.close()
实际应用场景
配置备份是网络管理的基本需求,以下示例展示了如何使用Napalm创建一个简单的配置备份系统,定期获取并保存网络设备的配置。
from napalm import get_network_driver
import os
import datetime
import concurrent.futures
import json
# 设备清单(实际应用中可从数据库或配置文件读取)
devices = [
{'hostname': '192.168.1.1', 'device_type': 'ios', 'username': 'admin', 'password': 'password'},
{'hostname': '192.168.1.2', 'device_type': 'eos', 'username': 'admin', 'password': 'password'}
]
# 创建备份目录
backup_dir = 'network_backups'
os.makedirs(backup_dir, exist_ok=True)
# 获取设备配置的函数
def backup_device_config(device_info):
driver = get_network_driver(device_info['device_type'])
try:
# 连接设备
with driver(
hostname=device_info['hostname'],
username=device_info['username'],
password=device_info['password']
) as device:
# 获取当前时间戳
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
# 设备名称(用于文件名)
hostname = device_info['hostname'].replace('.', '_')
# 获取设备基本信息
facts = device.get_facts()
real_hostname = facts['hostname']
# 获取运行配置
config = device.get_config()
running_config = config['running']
# 创建设备专用目录
device_dir = os.path.join(backup_dir, hostname)
os.makedirs(device_dir, exist_ok=True)
# 保存配置文件
filename = f"{real_hostname}_{timestamp}.cfg"
file_path = os.path.join(device_dir, filename)
with open(file_path, 'w') as f:
f.write(running_config)
# 保存设备信息
info_file = os.path.join(device_dir, 'device_info.json')
with open(info_file, 'w') as f:
json.dump(facts, f, indent=4)
returnf"成功备份 {real_hostname} 配置到 {file_path}"
except Exception as e:
returnf"备份 {device_info['hostname']} 失败: {str(e)}"
# 并行备份所有设备
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_device = {executor.submit(backup_device_config, device): device for device in devices}
for future in concurrent.futures.as_completed(future_to_device):
device = future_to_device[future]
try:
result = future.result()
print(result)
except Exception as e:
print(f"处理 {device['hostname']} 时发生异常: {str(e)}")
print(f"所有备份已保存到 {os.path.abspath(backup_dir)} 目录")
总结
Python Napalm库是网络自动化领域的一个强大工具,它通过提供统一的API接口解决了多厂商网络设备管理的复杂性。Napalm的核心优势在于其厂商无关的抽象层、强大的配置管理能力和全面的设备状态检索功能,使网络工程师能够编写一次代码,在不同厂商的设备上运行。通过本文介绍的基本功能和高级特性,网络工程师可以利用Napalm构建各种自动化解决方案,如配置管理系统、网络监控平台和合规性检查工具。
如果你觉得文章还不错,请大家 点赞、分享、留言 下,因为这将是我持续输出更多优质文章的最强动力!





















暂无评论内容