适当减少手机使用,有助于视力保护... 新的一年祝您:身体健康,阖家欢乐!~~

netmiko 测试之回显不全的解决

浏览量:1628 作者:admin 类别:: 学习笔记 更新时间:2022-08-21 13:21:35
from netmiko import ConnectHandler
from datetime import datetime
import time


# import logging
# logging.basicConfig(level=logging.DEBUG)    #开启debug模式

#'''
# send_commond  send_multiline  send_command_expect(兼容旧版) send_multiline_timing send_config_from_file send_config_set
# '''


device2 = {"host": "10.10.10.2",                   # 设备ip地址 必要参数
          "device_type":"hp_comware_telnet",       # ruijie_os 后面加_telnet  则使用telnet连接 # 设备类型 必要参数
          "username": "root",
          "password": "root",
          "port":None   ,                          #ssh 默认端口22 telnet默认端口23
           "session_log":'device_log.log'
          }

cmd2 ='''
sys
disp cu
'''

try:
    print('开始执行:',datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
    conn = ConnectHandler(**device2)
    #查看设备配置
    #send_command

    output = conn.send_command(cmd2, read_timeout=20.0,expect_string=r'#')#strip_command=False, strip_prompt=False,
    # output = conn.send_command_timing(cmd2, read_timeout=20.0)
    #expect_string=r'#' 华三交换机,要排除这#,否则回显不了 ,且多行命令容易回显不完整

    # output = conn.send_config_set(cmd2)  还是使用这个比较好。


    print(output)
    # 解决回显不完整的情况
    while '>' not in output[-5:]:
        # print('回显并未取完...,检测到回显结尾:<', output[-5:], '>')
        #方法一:再次发送命令
        # out = conn.send_command('\t', read_timeout=20.0, expect_string=r'#')

        # 方法二:接着读回显 ,推荐
        out1 = conn.read_channel()  # 接着读的回显是空...
        time.sleep(0.2) # 必须停一下,否则疯狂的读,虽然不影响结果,但读取的空白,会占屏...
        print(out1)
        output += out1

        if ']' in output[-5:] or '>' in output[-5:]:
            break

    # 输出内容
    print(output)
    print('执行完成:', datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
    #断开连接
    print(conn.command_echo_read(cmd2,20))
    conn.disconnect()
    print('断开连接')
except Exception as e:
    print(e)
    print('连接失败...')


关于博主
北京南城网络及弱电【张工】为您提供:
校园网、楼宇、村镇、园区,光纤网络覆盖,无线覆盖 华为|H3c|锐捷|爱快|panabit等品牌,路由、交换机、AC、AP 无源光网络(PON)设备,布署、调试、维运,等服务...
应急/临时光纤熔接...[藤仓22S]
博文为学习python所写:内容摘自网络、学习笔记、记事备忘。
QQ:872876353,欢迎交流学习、加Q注明来意。

诗文鉴赏

《满江红》· 岳飞
落星侵晓没,
残月半山低。
怒发冲冠,
凭栏处、潇潇雨歇。
抬望眼、仰天长啸,壮怀激烈。
三十功名尘与土,八千里路云和月。
莫等闲、白了少年头,
空悲切。
源于生活-记录日常
The site based on python 3 with flask...