file-type

Python Netmiko测试实践指南

ZIP文件

下载需积分: 9 | 3KB | 更新于2024-12-18 | 94 浏览量 | 0 下载量 举报 收藏
download 立即下载
Netmiko是一个基于paramiko的库,专门用于简化SSH连接到网络设备如路由器和交换机的过程。本项目主要通过编写Python脚本,以实现对网络设备的批量配置、状态检测、软件升级等自动化操作。Netmiko的设计初衷是为了解决网络工程师在进行网络设备管理时遇到的一些挑战,例如频繁的重复性命令输入和多设备配置同步问题。 Netmiko的特点主要包括: - 支持多厂商设备:Netmiko能够支持众多网络设备厂商,包括但不限于Cisco、Juniper、HP、Arista等。 - 简化SSH连接:通过预定义的函数和类,Netmiko使得通过SSH连接到网络设备变得异常简单,减少了编写大量样板代码的需求。 - 支持多命令执行:Netmiko支持通过一次SSH连接发送多个命令到网络设备,并能够收集返回的输出结果。 - 自动化脚本示例:Netmiko提供了多种自动化操作的示例脚本,帮助网络工程师快速开始他们的自动化项目。 - 异常处理机制:Netmiko内部集成了异常处理机制,确保在网络设备不可用或命令执行失败时能够提供清晰的错误信息。 对于Python程序员来说,掌握Netmiko库将大大提升网络设备管理的效率和可靠性。用户可以通过pip安装Netmiko,然后通过阅读官方文档或示例代码来了解如何使用这个库来自动化他们的网络任务。 此外,Netmiko_test项目中通常还会包含与实际网络环境中的设备进行交互的各种测试文件,这些测试文件会验证Netmiko库与不同设备的兼容性和稳定性。使用这些测试文件,网络工程师可以模拟不同的网络环境和场景,检验自己的自动化脚本是否能够在实际环境中可靠运行。 总之,Netmiko_test项目是网络工程师进行Python自动化测试的理想选择,它不仅简化了网络设备管理过程中的编程任务,而且通过开源社区的支持,持续推动网络自动化技术的发展和创新。"

相关推荐

filetype

我把日志级别改成了debug,首先权限,命令都是无问题,权限给的最高,命令我手动ssh登录控制台都可正常显示,也不会报错,但是这个程序命令执行在交换机后台日志看只正确了执行quit这条命令,其余都未执行,程序日志如下:2025-07-15 14:21:27,593 - DEBUG - read_channel: 2025-07-15 14:21:29,594 - DEBUG - read_channel: 2025-07-15 14:21:29,594 - DEBUG - write_channel: b'quit\n' 2025-07-15 14:21:29,594 - DEBUG - EOF in transport thread 2025-07-15 14:21:29,595 - INFO - 172.17.201.28 不是已知汇聚交换机,作为最终设备 2025-07-15 14:21:29,608 - DEBUG - starting thread (client mode): 0x7a64f2f0 2025-07-15 14:21:29,608 - DEBUG - Local version/idstring: SSH-2.0-paramiko_3.5.1 2025-07-15 14:21:29,609 - DEBUG - Remote version/idstring: SSH-1.99-Comware-5.20 2025-07-15 14:21:29,609 - INFO - Connected (version 1.99, client Comware-5.20) 2025-07-15 14:21:29,631 - DEBUG - === Key exchange possibilities === 2025-07-15 14:21:29,631 - DEBUG - kex algos: diffie-hellman-group-exchange-sha1, diffie-hellman-group14-sha1, diffie-hellman-group1-sha1 2025-07-15 14:21:29,631 - DEBUG - server key: ssh-rsa 2025-07-15 14:21:29,631 - DEBUG - client encrypt: aes128-cbc, 3des-cbc, des-cbc 2025-07-15 14:21:29,631 - DEBUG - server encrypt: aes128-cbc, 3des-cbc, des-cbc 2025-07-15 14:21:29,631 - DEBUG - client mac: hmac-sha1, hmac-sha1-96, hmac-md5, hmac-md5-96 2025-07-15 14:21:29,631 - DEBUG - server mac: hmac-sha1, hmac-sha1-96, hmac-md5, hmac-md5-96 2025-07-15 14:21:29,631 - DEBUG - client compress: none 2025-07-15 14:21:29,631 - DEBUG - server compress: none 2025-07-15 14:21:29,631 - DEBUG - client lang: <none> 2025-07-15 14:21:29,631 - DEBUG - server lang: <none> 2025-07-15 14:21:29,631 - DEBUG - kex follows: False 2025-07-15 14:21:29,631 - DEBUG - === Key exchange agreements === 2025-07-15 14:21:29,631 - DEBUG - Kex: diffie-hellman-group-exchange-sha1 2025-07-15 14:21:29,631 - DEBUG - HostKey: ssh-rsa 2025-07-15 14:21:29,631 - DEBUG - Cipher: aes128-cbc 2025-07-15 14:21:29,631 - DEBUG - MAC: hmac-sha1 2025-07-15 14:21:29,632 - DEBUG - Compression: none 2025-07-15 14:21:29,632 - DEBUG - === End of kex handshake === 2025-07-15 14:21:29,655 - DEBUG - Got server p (2048 bits) 2025-07-15 14:21:30,462 - DEBUG - kex engine KexGex specified hash_algo <built-in function openssl_sha1> 2025-07-15 14:21:30,462 - DEBUG - Switch to new keys ... 2025-07-15 14:21:30,463 - DEBUG - Adding ssh-rsa host key for 172.17.201.28: b'5e3ca5910d7cce29466ca9634fce6a23' 2025-07-15 14:21:30,486 - DEBUG - userauth is OK 2025-07-15 14:21:30,506 - INFO - Authentication (password) successful! 2025-07-15 14:21:30,507 - DEBUG - [chan 0] Max packet in: 32768 bytes 2025-07-15 14:21:30,528 - DEBUG - [chan 0] Max packet out: 32496 bytes 2025-07-15 14:21:30,528 - DEBUG - Secsh channel 0 opened. 2025-07-15 14:21:30,532 - DEBUG - [chan 0] Sesch channel 0 request ok 2025-07-15 14:21:30,536 - DEBUG - [chan 0] Sesch channel 0 request ok 2025-07-15 14:21:30,536 - DEBUG - write_channel: b'\n' 2025-07-15 14:21:30,638 - DEBUG - read_channel: ****************************************************************************** * Copyright (c) 2004-2013 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * * Without the owner's prior written consent, * * no decompiling or reverse-engineering shall be allowed. * ****************************************************************************** <CDSL_SW201.28_LBG> 2025-07-15 14:21:30,638 - DEBUG - Pattern found: (to continue|[>\]]) ****************************************************************************** * Copyright (c) 2004-2013 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * * Without the owner's prior written consent, * * no decompiling or reverse-engineering shall be allowed. * ****************************************************************************** <CDSL_SW201.28_LBG> 2025-07-15 14:21:30,939 - DEBUG - read_channel: <CDSL_SW201.28_LBG> 2025-07-15 14:21:30,939 - DEBUG - Clear buffer detects data in the channel 2025-07-15 14:21:31,540 - DEBUG - read_channel: 2025-07-15 14:21:31,540 - DEBUG - write_channel: b'\n' 2025-07-15 14:21:31,540 - DEBUG - read_channel: 2025-07-15 14:21:31,551 - DEBUG - read_channel: <CDSL_SW201.28_LBG> 2025-07-15 14:21:31,551 - DEBUG - Parenthesis found in pattern. pattern: (>|\]) This can be problemtic when used in read_until_pattern(). You should ensure that you use either non-capture groups i.e. '(?:' or that the parenthesis completely wrap the pattern '(pattern)' 2025-07-15 14:21:31,551 - DEBUG - Pattern found: (>|\]) <CDSL_SW201.28_LBG> 2025-07-15 14:21:31,852 - DEBUG - read_channel: 2025-07-15 14:21:31,852 - DEBUG - [find_prompt()]: prompt is <CDSL_SW201.28_LBG> 2025-07-15 14:21:31,852 - DEBUG - In disable_paging 2025-07-15 14:21:31,852 - DEBUG - Command: screen-length disable 2025-07-15 14:21:31,852 - DEBUG - write_channel: b'screen-length disable\n' 2025-07-15 14:21:31,852 - DEBUG - read_channel: 2025-07-15 14:21:31,863 - DEBUG - read_channel: s 2025-07-15 14:21:31,874 - DEBUG - read_channel: 2025-07-15 14:21:31,885 - DEBUG - read_channel: creen-length disable 2025-07-15 14:21:31,896 - DEBUG - read_channel: 2025-07-15 14:21:31,907 - DEBUG - read_channel: ^ % Unrecognized command found at '^' position. <CDSL_SW201.28_LBG> 2025-07-15 14:21:31,907 - DEBUG - Pattern found: (CDSL_SW201\.28_LBG) screen-length disable ^ % Unrecognized command found at '^' position. <CDSL_SW201.28_LBG 2025-07-15 14:21:31,907 - DEBUG - screen-length disable ^ % Unrecognized command found at '^' position. <CDSL_SW201.28_LBG 2025-07-15 14:21:31,907 - DEBUG - Exiting disable_paging 2025-07-15 14:21:31,907 - INFO - 成功连接 172.17.201.28 2025-07-15 14:21:31,907 - DEBUG - write_channel: b'screen-length disable\n' 2025-07-15 14:21:32,008 - DEBUG - read_channel: screen-length disable ^ % Unrecognized command found at '^' position. <CDSL_SW201.28_LBG> 2025-07-15 14:21:32,108 - DEBUG - read_channel: 2025-07-15 14:21:34,109 - DEBUG - read_channel: 2025-07-15 14:21:34,610 - DEBUG - write_channel: b'system-view\n' 2025-07-15 14:21:34,711 - DEBUG - read_channel: system-view ^ % Unrecognized command found at '^' position. <CDSL_SW201.28_LBG> 2025-07-15 14:21:34,812 - DEBUG - read_channel: 2025-07-15 14:21:36,813 - DEBUG - read_channel: 2025-07-15 14:21:37,814 - INFO - 172.17.201.28 是 H3C V5 交换机,使用特殊绑定流程 2025-07-15 14:21:37,814 - DEBUG - write_channel: b'display mac-address 6c0b-5e47-7f89\n' 2025-07-15 14:21:37,915 - DEBUG - read_channel: display mac-address 6c0b-5e47-7f89 ^ % Unrecognized command found at '^' position. <CDSL_SW201.28_LBG> 2025-07-15 14:21:38,016 - DEBUG - read_channel: 2025-07-15 14:21:40,018 - DEBUG - read_channel: 2025-07-15 14:21:40,018 - INFO - MAC地址查询结果: ^ % Unrecognized command found at '^' position. 2025-07-15 14:21:40,018 - ERROR - 在 172.17.201.28 上未找到 MAC 6c0b-5e47-7f89 对应的端口 2025-07-15 14:21:40,019 - DEBUG - write_channel: b'\n' 2025-07-15 14:21:40,120 - DEBUG - read_channel: <CDSL_SW201.28_LBG> 2025-07-15 14:21:40,221 - DEBUG - read_channel: 2025-07-15 14:21:42,222 - DEBUG - read_channel: 2025-07-15 14:21:42,222 - DEBUG - write_channel: b'quit\n' 2025-07-15 14:21:42,222 - DEBUG - EOF in transport thread