首页 > 编程语言 >Python小程序(二):巡检H3C网络设备

Python小程序(二):巡检H3C网络设备

时间:2022-08-17 12:23:51浏览次数:68  
标签:H3C Python ip host device 网络设备 txt display

Python小程序(二):巡检H3C网络设备

读取设备列表或txt文件,自动化巡检H3C设备。

import netmikofrom netmiko import ConnectHandler, NetmikoBaseExceptionimport datetime,time

def H3C_Device_Connect():
    h3c_device = {
        'device_type': 'hp_comware',
        'host': host_ip,
        'username': username,
        'password': password,
        'port': 22,  # optional, defaults to 22
        'secret': 'secret',  # optional, defaults to ''
        'timeout': 180
    }

    #config_commands = open('device_config.txt', 'r', encoding='utf8')
    config_commands = ['screen-length disable',
                       'display ip routing-table',
                       'display device',
                       'dispaly version',
                       'display memory summary',
                       'display fan verbose',
                       'display interface brief',
                       'dir',
                       'display logbuffer',
                       'display  current-configuration'
                       ]

    try:
        with ConnectHandler(**h3c_device) as H3C_SSH_Device:
            print(f'successfully logged in ', host_ip)
            output = H3C_SSH_Device.send_config_set( config_commands, delay_factor=5, enter_config_mode = False)
            #print(output)
            host_txt = open(host_ip + '.txt', 'w')
            host_txt.write(output)
    except:
        print('Not Connect device is ', host_ip)

try:
    with open('H3C_device.txt', 'r', encoding='utf8') as host_file:
        for host_info in host_file:
            line = host_info.strip('\n')
            host_ip, host_port, username, password = line.split(',')
            H3C_Device_Connect()
except FileNotFoundError as e:
    print(e)

必坑要点:

H3C设备在'device_type'推荐使用`hp_comware`,而不是使用'huawei'。当使用'huawei'时,会出现'display current-configuration'显示不全的问题。

 

标签:H3C,Python,ip,host,device,网络设备,txt,display
From: https://www.cnblogs.com/ColoFly/p/16594658.html

相关文章

  • Python逆向爬虫之正则表达式
    Python逆向爬虫之正则表达式字符串是我们在编程的时候很常用的一种数据类型,检查会在字符串里面查找一些内容,对于比较简单的查找,字符串里面就有一些内置的方法可以处理,对于......
  • Python 字典排序
    字典是“键-值对”的无序可变序列在实际运用中,对字典进行排序是一个比较常见的操作,主要用到了python内置函数sorted(),该函数可以对所有可迭代的对象进行排序操作。语法(pyth......
  • 批量产生文件夹(Python)
    1、指定文件夹路径,在该路径下批量生成指定名称(具有一定的规律性)的文件夹#导入OS库importosFilePath=""#指定文件夹路径NumSets=#文件夹个数defBatProFile_......
  • 跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算
    摘要:本篇文章结合灰度三维图像讲解图像顶帽运算和图像黑猫运算,通过Python调用OpenCV函数实现。本文分享自华为云社区《[Python图像处理]十三.基于灰度三维图的图像顶帽运......
  • python自动化上传文件
    定位上传文件的按钮报错:selenium.common.exceptions.ElementNotInteractableException:Message:elementnotinteractable修改为执行js代码正常,如下:elem_js=self._dr......
  • python打包成EXE文件
    参考博客:https://www.300.cn/itzspd/609175.htmlPython打包工具PyInstaller的安装与pycharm配置支持PyInstaller详细方法windows系统下安装Pyinstallercmd下输入指令pi......
  • python菜鸟学习: 8. 集合基础知识
    #-*-coding:utf-8-*-#列表的特性:1,去重;2.关系测试list1=[1,4,6,8,7,8,9]#集合去重list2=set(list1)print(list2)list4=[1,3,5,7,9,11]list3=set(l......
  • python_socket
    importsocketdeftarget_tcp(host,port):"""建立tcp连接"""client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#创建socket对象client.co......
  • Python修改windows键盘映射
    新电脑环境的处理之一是修改键盘映射,主Ctrl键使用CapsLockCapsLock改为LeftCtrlRightCtrl改为CapsLockLeftCtrl改为RightCtrl原来使用KeybMap这个软件修改,由于自己修改......
  • python 中统计绵羊 ARS-UI_Ramb_v2.0)参考基因组中GC含量及每条染色体的长度
     001、方法1root@PC1:/home/test#lsa.fastatest.pyroot@PC1:/home/test#head-n5a.fasta##参考基因组文件>NC_056054.1OvisariesstrainOA......