首页 > 其他分享 >netmiko批量操作华为设备

netmiko批量操作华为设备

时间:2022-11-14 22:13:48浏览次数:36  
标签:username netmiko 批量 ip list 华为 print password row

import netmiko
import openpyxl
from netmiko import ConnectHandler
from openpyxl import load_workbook
from concurrent.futures import ThreadPoolExecutor

# 从excel中获取ip列表
def read_excel_ip():
    """
    获取登录设备所需的ip地址
    """
    wb = load_workbook(filename='data.xlsx')
    dev_info = wb['Sheet1']
    ip_list = []
    for rows in range(2, dev_info.max_row + 1 ):
        row = dev_info.cell(rows, 1).value  # 获取第一列值
        ip_list.append(row)  # 把第一列的每一行数据都返回给ip_list列表
    return ip_list

def read_username():
    """
    获取登录设备所需的用户名
    """
    wb = load_workbook(filename='data.xlsx')
    dev_info = wb['Sheet1']
    username_list = []
    for rows in range(2, dev_info.max_row + 1 ):
        row = dev_info.cell(rows, 2).value  # 获取第2列值
        username_list.append(row)  # 把第2列的每一行数据都返回给user_list列表
    return username_list

def read_password():
    """
    获取登录设备所需的密码
    """
    wb = load_workbook(filename='data.xlsx')
    dev_info = wb['Sheet1']
    passwrod_list = []
    for rows in range(2, dev_info.max_row + 1 ):
        row = dev_info.cell(rows, 3).value  # 获取第3列值
        passwrod_list.append(row)  # 把第3列的每一行数据都返回给passwor_list列表
    return passwrod_list

# 执行netmiko ssh操作
def ssh(ip,username,password):
    """
    通过netmiko登录设备,调用之前定义函数返回的ip,username,password
    将配置文件自定义命名并保存到指定目录
    """
    try:
        devices = {
            'device_type': 'huawei',
            'ip': ip,
            'username': username,
            'password': str(password)
        }
        connect = ConnectHandler(**devices)
        print('Connect to ' + devices['ip'])
        output = connect.send_config_from_file("cmd.txt", enter_config_mode=False)
        print(output)
        #输出结果保存为文本。
        with open(str(ip) + ".txt" , "w", encoding="utf=8") as log:
            log.write(output)

    except netmiko.exceptions.NetmikoTimeoutException:
        print(ip + " 登录超时")
        with open ("登录失败列表", "a+" ,encoding="utf-8") as failed_ip:
            failed_ip.write("\n" + ip)

    except netmiko.exceptions.NetmikoAuthenticationException:
        with open ("登录失败列表", "a+" ,encoding="utf-8") as failed_ip:
            failed_ip.write("\n" + ip)


ip = read_excel_ip()  # 将ip_list的值返回给read_excel_ip方法。
print(ip)
username = read_username()
print(username)
password = read_password()
print(password)
pool = ThreadPoolExecutor(10)
for ips, usernames, passwords in zip(ip, username, password):#多线程批量执行。
    pool.submit(ssh,ips,usernames,passwords) #接收各个函数的值 在ssh中调用



pool.shutdown(True)

"""
for i in ip:  # 取ip_list的ip,进行循环登录。
    print(ssh(i))
with open ("登录失败列表" , "r", encoding="utf=8") as result:
    print("\n以下ip登录失败:")
    print(result.read())
"""

 

标签:username,netmiko,批量,ip,list,华为,print,password,row
From: https://www.cnblogs.com/yc-tec/p/16890638.html

相关文章

  • mongodb增删改查 及批量操作(二)
    1.增加insert插入多笔数据db.stu.insert([{_id:3,sn:'003',name:'zhangfei'},{_id:4,sn:'004',name:'guanyu'},{sn:'005',name:'liubei'}])增加复杂的文档db.stu.insert({na......
  • nuxt3批量引入svg文件
    nuxt3引入svg的方法和viet的差不多,只是细节上有些不同同样是两步,引入svg组件和引入svg文件第一步:引入svg组件在components文件夹下创建SvgIcon.vue组件<template>......
  • NFV简介-华为
    转自:https://info.support.huawei.com/info-finder/encyclopedia/zh/NFV.html1.什么是NFV 网络功能虚拟化(NetworkFunctionsVirtualization,NFV)是一种关于网络架构的概......
  • 批量转换MP3
    find.-name"*mp4"|grepmp4>.0001whilereadlinedoecho"$line"":copying-----"Vpath_name="$line"cp-rp"$line".done<.0001ls|grepm......
  • SqlBulkCopy(批量复制)方法,高性能批量插入数据
    https://blog.51cto.com/u_15162069/2751372https://blog.csdn.net/five_s/article/details/115213102SQLServer批量插入数据方案SqlBulkCopy相较于Insert语句而言......
  • chrome浏览器 开发者工具F12中某网站的sources下的源码如何批量保存
    1.常用保存Sources源码的两种方法1.1单个文件直接右键另存为1.2单个页面保存网页,就会把引用到的所有文件下载下来如果页面很多,文件也很多,静态资源也很多,那么得一......
  • 28、批量实现txt文件内容合并
    题目:  在many_org文件夹中有三个.txt文件,如何将三个文件的内容整理到一个文件里?思路:  1、遍历路径下的所有文件。  2、判断出.txt文件,将其所有内容保存至新列表......
  • 文件批量重命名
    批量重命名一、背景我从网上下载了一部电视剧《琅琊榜》,存放在了E:/琅琊榜目录下:但有没有发现片名好长好长,这严重影响了我的观看体验,我该怎样才能将片名中的「未删减......
  • 发现一款音频批量下载小工具,喜欢的喜马拉雅音频都能直接下到自己的硬盘里
    最近发现了一款音频下载工具,解决了我多年的困惑。因为喜欢喜马拉雅的好多连载小说,所以购买了VIP,手机里实在装不下了,但又没法存储在硬盘里。最近用了一款音频下载小工具,找......
  • 报文解压缩(华为机试)
    #include<iostream>#include<string>#include<stack>#include<algorithm>usingnamespacestd;stringop(intnum,string&str){ stringtemp; for(inti=0;i......