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