
本教程详细阐述了如何利用python的netmiko库自动化配置cisco路由器,重点解决ssh连接超时及配置命令错误的问题。文章将指导读者正确构建设备连接参数,优化配置命令发送方式(特别是避免手动输入`enable`和`configure terminal`),并提供完整的代码示例,涵盖接口、ospf配置、配置保存与比较,确保网络自动化脚本的稳定与高效运行。
在网络管理中,自动化配置能够极大地提高效率并减少人为错误。Python的Netmiko库是实现这一目标的重要工具,它提供了一个简单而强大的接口,用于通过SSH或Telnet连接到各种网络设备并执行命令。然而,在使用Netmiko时,一些常见的误区可能导致连接超时或配置失败。本文将深入探讨如何正确使用Netmiko进行Cisco路由器的SSH连接与配置,并优化配置命令的发送方式。
Netmiko的核心优势之一是它能够智能地处理设备的不同配置模式。当使用net_connect.send_config_set()方法发送配置命令列表时,Netmiko会自动:
因此,在send_config_set()的命令列表中,不应手动包含en(或enable)和conf t(或configure terminal)命令。手动添加这些命令会导致Netmiko尝试重复进入已处于的模式,从而可能引发命令解析错误、延迟或连接超时。
错误的配置命令示例(应避免):
loopback_config = [
'en\n' # 错误:Netmiko会自动处理
'conf t\n' # 错误:Netmiko会自动处理
'interface Loopback0\n',
'ip address 192.168.57.101 255.255.255.0\n',
'exit\n'
]正确的配置命令示例:
loopback_config = [
'interface Loopback0',
'ip address 192.168.57.101 255.255.255.0',
'exit'
]请注意,命令末尾通常不需要\n,Netmiko会正确发送。
在使用Netmiko连接设备之前,需要定义一个包含设备连接信息的字典。这个字典是ConnectHandler类的关键输入。
import getpass
from netmiko import ConnectHandler
import logging
logging.basicConfig(level=logging.INFO)
def get_device_info():
"""
获取用户输入的设备连接信息并构建设备字典。
"""
host = '192.168.56.101' # 目标设备的IP地址
username = input('请输入您的用户名: ')
password = getpass.getpass('请输入您的密码: ')
secret = getpass.getpass('请输入特权模式密码 (如果需要): ') # 特权模式密码,通常为enable密码
# 用户选择连接类型
while True:
choice = input('请选择连接类型 (telnet 或 ssh): ').lower()
if choice == 'telnet':
device_type = 'cisco_ios_telnet'
port = 23
break
elif choice == 'ssh':
device_type = 'cisco_ios'
port = 22
break
else:
logging.warning('无效选择,请重新输入 "telnet" 或 "ssh"。')
device = {
'device_type': device_type,
'host': host,
'username': username,
'password': password,
'secret': secret,
'port': port,
'timeout': 100, # 增加超时时间以应对潜在的网络延迟
}
return device在device字典中:
根据上述原则,我们可以构造正确的配置命令列表,实现Loopback接口、物理接口和OSPF协议的配置。
def configure_device(net_connect):
"""
向设备发送配置命令。
"""
logging.info('正在发送配置命令...')
# Loopback接口配置
loopback_config = [
'interface Loopback0',
'ip address 192.168.57.101 255.255.255.0',
'no shutdown' # 确保接口启用
]
# ACL配置 (示例,根据需要调整)
acl_config = [
'ip access-list extended MY_ACL',
'permit ip 192.168.56.130 0.0.0.255 any', # 注意ACL中的通配符掩码
'deny ip any any',
'exit'
]
# 物理接口GigabitEthernet0/0配置
interface0_config = [
'interface GigabitEthernet0/0',
'ip address 192.168.58.101 255.255.255.0',
'no shutdown'
]
# 物理接口GigabitEthernet0/1配置
interface1_config = [
'interface GigabitEthernet0/1',
'ip address 192.168.59.101 255.255.255.0',
'no shutdown'
]
# OSPF配置
ospf_config = [
'router ospf 1', # OSPF进程ID为1
'network 192.168.57.0 0.0.0.255 area 0', # 宣告Loopback0所在网络
'network 192.168.58.0 0.0.0.255 area 0', # 宣告GigabitEthernet0/0所在网络
'network 192.168.59.0 0.0.0.255 area 0', # 宣告GigabitEthernet0/1所在网络
'exit'
]
# 合并所有配置命令
all_configs = loopback_config + acl_config + interface0_config + interface1_config + ospf_config
# 使用send_config_set发送配置
output = net_connect.send_config_set(all_configs)
print(output)
logging.info('配置命令发送完毕。')注意事项:
自动化脚本通常需要能够保存当前运行配置,并可能与本地存储的配置进行比较,以检测差异。
import difflib
def save_config_to_file(config, filename):
"""
将配置内容保存到文件。
"""
with open(filename, 'w') as config_file:
config_file.write(config)
logging.info(f'配置已保存到 {filename}')
def show_differences(config1_content, config2_content, name1="Config 1", name2="Config 2"):
"""
显示两个配置之间的差异。
"""
difference = difflib.Differ()
diff = list(difference.compare(config1_content.splitlines(keepends=True), config2_content.splitlines(keepends=True)))
logging.warning(f'配置差异 ({name1} vs {name2}):')
for line in diff:
if line.startswith('- '):
logging.info(f'- {line[2:].strip()}') # 仅在Config 1中存在
elif line.startswith('+ '):
logging.info(f'+ {line[2:].strip()}') # 仅在Config 2中存在
elif line.startswith('? '):
pass # 忽略difflib的提示行结合上述组件,我们可以构建一个完整的Netmiko自动化脚本。
import logging
import getpass
import difflib
from netmiko import ConnectHandler
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def save_config_to_file(config, filename):
"""将配置内容保存到文件。"""
with open(filename, 'w') as config_file:
config_file.write(config)
logging.info(f'配置已保存到 {filename}')
def show_differences(config1_content, config2_content, name1="Config 1", name2="Config 2"):
"""显示两个配置之间的差异。"""
difference = difflib.Differ()
diff = list(difference.compare(config1_content.splitlines(keepends=True), config2_content.splitlines(keepends=True)))
logging.warning(f'配置差异 ({name1} vs {name2}):')
for line in diff:
if line.startswith('- '):
logging.info(f'- {line[2:].strip()}')
elif line.startswith('+ '):
logging.info(f'+ {line[2:].strip()}')
elif line.startswith('? '):
pass
def configure_device(net_connect):
"""向设备发送配置命令。"""
logging.info('正在发送配置命令...')
loopback_config = [
'interface Loopback0',
'ip address 192.168.57.101 255.255.255.0',
'no shutdown'
]
acl_config = [
'ip access-list extended MY_ACL',
'permit ip 192.168.56.130 0.0.0.255 any',
'deny ip any any',
'exit'
]
interface0_config = [
'interface GigabitEthernet0/0',
'ip address 192.168.58.101 255.255.255.0',
'no shutdown'
]
interface1_config = [
'interface GigabitEthernet0/1',
'ip address 192.168.59.101 255.255.255.0',
'no shutdown'
]
ospf_config = [
'router ospf 1',
'network 192.168.57.0 0.0.0.255 area 0',
'network 192.168.58.0 0.0.0.255 area 0',
'network 192.168.59.0 0.0.0.255 area 0',
'exit'
]
all_configs = loopback_config + acl_config + interface0_config + interface1_config + ospf_config
output = net_connect.send_config_set(all_configs)
print(output)
logging.info('配置命令发送完毕。')
def main():
host = '192.168.56.101'
username = input('请输入您的用户名: ')
password = getpass.getpass('请输入您的密码: ')
secret = getpass.getpass('请输入特权模式密码 (如果需要): ')
while True:
choice = input('请选择连接类型 (telnet 或 ssh): ').lower()
if choice == 'telnet':
device_type = 'cisco_ios_telnet'
port = 23
break
elif choice == 'ssh':
device_type = 'cisco_ios'
port = 22
break
else:
logging.warning('无效选择,请重新输入 "telnet" 或 "ssh"。')
device = {
'device_type': device_type,
'host': host,
'username': username,
'password': password,
'secret': secret,
'port': port,
'timeout': 100,
}
try:
# 使用 'with' 语句确保连接正确关闭
with ConnectHandler(**device) as net_connect:
logging.info('连接已建立。')
# 发送配置命令
configure_device(net_connect)
# 保存运行配置
logging.info('正在获取设备的运行配置...')
running_configuration = net_connect.send_command('show running-config')
local_config_file_name = 'local_config.txt'
save_config_to_file(running_configuration, local_config_file_name)
logging.info(f'运行配置已保存到 {local_config_file_name}')
# 比较配置
try:
with open(local_config_file_name, 'r') as local_config_file:
local_config = local_config_file.read()
if running_configuration and local_config:
# 简单比较,忽略空白字符和换行符差异
if running_configuration.strip() == local_config.strip():
logging.info('运行配置与本地配置一致。')
else:
logging.warning('运行配置与本地配置不匹配。')
show_differences(local_config, running_configuration, "本地配置", "设备运行配置")
else:
logging.error('无法获取配置内容进行比较。')
except FileNotFoundError:
logging.error(f'本地配置文件 ({local_config_file_name}) 未找到。')
except Exception as e:
logging.error(f'发生错误: {e}')
finally:
logging.info('连接已终止。') # 'with' 语句会自动处理断开连接
if __name__ == "__main__":
main()通过遵循这些最佳实践,您可以构建出高效、稳定且易于维护的Netmiko自动化脚本,从而简化Cisco路由器的配置管理任务。
以上就是使用Netmiko自动化配置Cisco路由器:SSH连接与命令优化的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号