from concurrent.futures import ThreadPoolExecutor import netmiko import os from threading import Lock import pandas as pd class net_dev(): def __init__(self,excel_name): try : os.mkdir("./log") except: pass self.excel_name = excel_name self.list = [] # 空列表存储设备信息数据 self.pool = ThreadPoolExecutor(10) # 初始化线程数量 self.lock = Lock() # 添加线程锁,避免写入数据丢失 self.path = ("./log") # 创建保存log路径 self.mult_config=[] # 创建列表,保存多条命令。用于批量执行命令 def get_dev_info(self): # 获取sheet(设备信息)的dataframe. df = pd.read_excel(self.excel_name,sheet_name="设备信息") self.list = df.to_dict(orient="records") # 将数据打印出来,已字典存储的列表数据 #mult_conf = df["mult_command"].values.tolist() # 取一列的值生成列表 print(self.list) # 获取sheet(CMD)的dataframe df1 = pd.read_excel(self.excel_name,sheet_name="CMD") result1 = df1.to_dict(orient="list") # 将数据打印出来,将一列的数据存为一个字典 self.mult_config = result1["mult_command"] print(self.mult_config) def mult_cmd_in(self,ip,user,dev_type,passwd): try: devices = { 'device_type': dev_type, # 锐捷os:ruijie_os, 华三:hp_comware 中兴:zte_zxros 'ip': ip, 'username': user, 'password': passwd, } connect_dev = netmiko.ConnectHandler(**devices) cmd_out = connect_dev.send_config_set(self.mult_config,enter_config_mode=False) with open (ip + ".txt", "w",encoding="utf-8") as tmp_fle: tmp_fle.write(cmd_out) print(ip + " 执行成功") except netmiko.exceptions.NetmikoAuthenticationException: self.lock.acquire() with open("登录失败列表", "a", encoding="utf-8") as failed_ip: failed_ip.write(ip + " 用户名密码错误\n") print(ip + " 用户名密码错误") self.lock.release() except netmiko.exceptions.NetmikoTimeoutException: self.lock.acquire() with open("登录失败列表", "a", encoding="utf-8") as failed_ip: failed_ip.write(ip + " 登录超时\n") print(ip + " 登录超时") self.lock.release() def main(self): for dev_info in self.list: #print(dev_info) ip = dev_info["ip"] #print(ip) user = dev_info["user"] dev_type = dev_info["dev_type"] passwd = dev_info["password"] self.pool.submit(self.mult_cmd_in,ip,user,dev_type,passwd) os.chdir(self.path) self.pool.shutdown(True) yc_use = net_dev("设备信息表.xlsx") yc_use.get_dev_info() yc_use.main()
标签:info,name,netmiko,ip,self,dev,网络设备,pandas,mult From: https://www.cnblogs.com/yc-tec/p/17053420.html