Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
使用Netmiko自动化配置Cisco路由器:SSH连接与命令优化_创想鸟

使用Netmiko自动化配置Cisco路由器:SSH连接与命令优化

使用Netmiko自动化配置Cisco路由器:SSH连接与命令优化

本教程详细阐述了如何利用python的netmiko库自动化配置cisco路由器,重点解决ssh连接超时及配置命令错误的问题。文章将指导读者正确构建设备连接参数,优化配置命令发送方式(特别是避免手动输入`enable`和`configure terminal`),并提供完整的代码示例,涵盖接口、ospf配置、配置保存与比较,确保网络自动化脚本的稳定与高效运行。

使用Netmiko自动化配置Cisco路由器:SSH连接与命令优化

在网络管理中,自动化配置能够极大地提高效率并减少人为错误。Python的Netmiko库是实现这一目标的重要工具,它提供了一个简单而强大的接口,用于通过SSH或Telnet连接到各种网络设备并执行命令。然而,在使用Netmiko时,一些常见的误区可能导致连接超时或配置失败。本文将深入探讨如何正确使用Netmiko进行Cisco路由器的SSH连接与配置,并优化配置命令的发送方式。

1. 理解Netmiko的配置模式处理

Netmiko的核心优势之一是它能够智能地处理设备的不同配置模式。当使用net_connect.send_config_set()方法发送配置命令列表时,Netmiko会自动:

进入特权执行模式(如果需要,通过发送enable命令并提供secret密码)。进入全局配置模式(通过发送configure terminal命令)。逐条发送配置列表中的命令。在命令发送完毕后,自动退出全局配置模式。

因此,在send_config_set()的命令列表中,不应手动包含en(或enable)和conf t(或configure terminal)命令。手动添加这些命令会导致Netmiko尝试重复进入已处于的模式,从而可能引发命令解析错误、延迟或连接超时。

错误的配置命令示例(应避免):

loopback_config = [    'enn'  # 错误:Netmiko会自动处理    'conf tn' # 错误:Netmiko会自动处理    'interface Loopback0n',    'ip address 192.168.57.101 255.255.255.0n',    'exitn']

正确的配置命令示例:

loopback_config = [    'interface Loopback0',    'ip address 192.168.57.101 255.255.255.0',    'exit']

请注意,命令末尾通常不需要n,Netmiko会正确发送。

2. 构建设备连接参数

在使用Netmiko连接设备之前,需要定义一个包含设备连接信息的字典。这个字典是ConnectHandler类的关键输入。

琅琅配音 琅琅配音

全能AI配音神器

琅琅配音 208 查看详情 琅琅配音

import getpassfrom netmiko import ConnectHandlerimport logginglogging.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字典中:

device_type: 指定设备类型,如cisco_ios(SSH)或cisco_ios_telnet(Telnet)。host: 设备的IP地址。username, password: 用于登录设备的凭据。secret: 进入特权模式(enable)所需的密码。port: 连接端口,SSH默认为22,Telnet默认为23。timeout: 连接或命令执行的超时时间(秒),适当增加有助于避免因网络延迟导致的超时错误。

3. 实现接口与OSPF配置

根据上述原则,我们可以构造正确的配置命令列表,实现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('配置命令发送完毕。')

注意事项:

ACL中的255.255.255.0通常表示子网掩码,但在Cisco扩展ACL的permit/deny ip语句中,需要使用通配符掩码(wildcard mask),例如0.0.0.255对应255.255.255.0的子网掩码。no shutdown命令对于接口的启用至关重要。OSPF的network命令需要指定网络地址、通配符掩码和区域ID。

4. 配置保存与比较

自动化脚本通常需要能够保存当前运行配置,并可能与本地存储的配置进行比较,以检测差异。

import difflibdef 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的提示行

5. 完整的自动化脚本示例

结合上述组件,我们可以构建一个完整的Netmiko自动化脚本。

import loggingimport getpassimport difflibfrom 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('? '):            passdef 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()

6. 总结与注意事项

Netmiko智能处理模式切换:核心要点是Netmiko的send_config_set()方法会自动处理enable和configure terminal。避免在命令列表中重复这些命令是解决连接超时和配置错误的关键。错误处理:使用try…except块来捕获可能发生的网络连接或命令执行错误,并提供有用的日志信息。with语句:使用with ConnectHandler(…) as net_connect:结构可以确保Netmiko连接在代码块执行完毕后(无论是否发生异常)自动关闭,避免资源泄露。因此,在with块外部显式调用net_connect.disconnect()是多余且可能导致net_connect未定义的错误。超时设置:根据网络环境和设备响应速度,适当调整timeout参数可以提高脚本的健壮性。配置验证:在发送配置后,通过send_command(‘show running-config’)获取配置并与预期配置进行比较,是验证配置是否成功应用的有效方法。日志记录:良好的日志记录有助于调试和追踪脚本执行过程。

通过遵循这些最佳实践,您可以构建出高效、稳定且易于维护的Netmiko自动化脚本,从而简化Cisco路由器的配置管理任务。

以上就是使用Netmiko自动化配置Cisco路由器:SSH连接与命令优化的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/596899.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
CentOS上如何配置Zookeeper的安全策略
上一篇 2025年11月10日 18:40:02
苹果16使用技巧新手怎么用
下一篇 2025年11月10日 18:40:12

相关推荐

发表回复

登录后才能评论
关注微信