from concurrent.futures import ThreadPoolExecutor
import time
import netmiko
import os
from threading import Lock
import openpyxl

class net_dev():

def __init__(self,excel_name):
try :
os.mkdir("./log")
except:
pass
self.excel_name = excel_name
self.list = [] # 空列表存储设备信息数据
self.dic = {} # 空字典存储设备信息
self.pool = ThreadPoolExecutor(10) # 初始化线程数量
self.lock = Lock() # 添加线程锁,避免写入数据丢失
self.path = ("./log") # 创建保存log路径
self.mult_config=[] # 创建列表,保存多条命令。用于批量执行命令

def get_dev_info(self):
wb = openpyxl.load_workbook(filename=self.excel_name)
sheet = wb[wb.sheetnames[0]] # 使用sheet的第一页作为工作sheet
max_row = sheet.max_row # 定义最大行
max_colum = sheet.max_column # 定义最大列
row_data = list(sheet.rows) #遍历每一行数据
header_key = [] # 创建空列表存储第一行数据作为字典的key.
for data in range(1,max_colum+1): # 循环查找第一行,最大列的数据.最大列为max_colum+1
header_info = sheet.cell(row=1,column=data).value
header_key.append(header_info)
#print(header_key)
for row in row_data:
dev_info = [] # 创建列表,存储每行的数据
for i in row:
dev_info.append(i.value)
#print(dev_info)
self.dic = dict(zip(header_key,dev_info)) # 将key和每行的数据打包成字典,存储在字典中
#print(self.dic)
self.list.append(self.dic) #将每行的字典数据存储在初始化的列表中。
self.list.pop(0) # 去除第一行的数据
#print (self.list)
for mult_config_tmp in self.list:
#print(mult_config_tmp)
mult_config = mult_config_tmp["mult_command"]
if mult_config != None:
self.mult_config.append(mult_config)
#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):
print(self.list)
print(self.mult_config)
for dev_info in self.list:
#print(dev_info)
if dev_info["ip"] != None:
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. 欢迎使用 MWeb
  2. Selenium2+python自动化12-操作元素(键盘和鼠标事件)
  3. centos 下 django 1.8 配置好后 admin 后台无法显示 样式解决办法
  4. [转] Java中的访问控制
  5. Oracle游标循环更新数据案例
  6. c++,函数名不要和某个类名相同 (syntax error : missing ';' before identifier....)
  7. 使用Mybatis进行多表联查操作
  8. [转载]解决sudo: sorry, you must have a tty to run sudo
  9. ORACLE数据库SQL优化 not in 与not exits
  10. openjudge(四)
  11. java随笔1 Ctrl+1补全
  12. 1.Solr介绍
  13. 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)
  14. git 代码上传至远程仓库&从远程库克隆到本地
  15. Spring mvc中DispatcherServlet详解
  16. Oracle管理监控之使用utl_mail自动邮件报警配置
  17. linux 下 chkconfig安装与使用详解
  18. poi复杂excel的实现
  19. yii2项目实战-访问控制过滤器ACF讲解
  20. [转载]关于python字典类型最疯狂的表达方式

热门文章

  1. Jemeter参数
  2. TamperMonkey油猴脚本获取
  3. NetCoreWebApi3.0-------MiniProfiler使用教程
  4. Vue19 常用指令及自定义指令
  5. Linux07-常用命令-权限
  6. vue+.net入门级书签项目
  7. 线段树优化DP学习笔记 & JZOJ 孤独一生题解
  8. Os-ByteSec
  9. trollcave-v1-2
  10. $(...).bootstrapTable is not a function (已解决)