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()

最新文章

  1. CSS3实现鼠标移动到图片上图片变大
  2. String的高级用法(String.Format)
  3. ALLOCATE语句分配FORTRAN动态数组方法(转自http://blog.csdn.net/zhuxianjianqi/article/details/8067174)
  4. while循环问题(老师询问问题,学生回答。学生会了可以放学,或者老师讲了10遍,还是没有会的,被迫无奈也要放学。)
  5. 关于数据库表中的索引及索引列的CRUD
  6. IOS 作业项目(2) 画图(保存,撤销,笔粗细设定功能)
  7. Android记录6--ViewPage+Fragment的使用例子
  8. Solr使用初探——Solr的安装环境与配置
  9. 贪心-poj-3040-Allowance
  10. Qgis插件开发之Qgis源码学习
  11. 第十章 MyBatis入门
  12. ORACLE - 管理控制文件
  13. hihocoder1388 Periodic Signal
  14. nginx 配置域名转发
  15. Project篇:项目1
  16. 回顾 Exchange 2007 SCC 安装-供需要的人参考!
  17. 8 -- 深入使用Spring -- 2...6 Spring 4.0 增强的自动装配和精确装配
  18. HTML-锚点-JS跳转锚点
  19. update select 多字段
  20. Rabbit简单测试实例

热门文章

  1. StringBuilder类-toString方法
  2. Spring异步Async和事务Transactional注解
  3. WinNTSetup V5.3.0 Bata5 单文件版
  4. 【CTO变形记】高维视角,跳出“农场主与火鸡”
  5. 安装和配置Java开发环境JDK
  6. CSS常用属性(3)
  7. windwos提权漏洞CVE-2023-21746复现(LocalPotato)
  8. JWT的原理及使用
  9. 大规模 IoT 边缘容器集群管理的几种架构-4-Kubeedge
  10. 题解 [SCOI2005]王室联邦