import time
import socket
import threading def device_info():
ip_list = []
name_list = []
user_list = []
passwd_list = []
f = open('devices_list.txt',encoding='UTF-8')
for line in f.readlines():
line_s = line.split()
device_ip = line_s[0]
device_name = line_s[1]
username = line_s[2]
passwd = line_s[3]
ip_list.append(device_ip)
name_list.append(device_name)
user_list.append(username)
passwd_list.append(passwd)
f.close()
return ip_list,name_list,user_list,passwd_list def ssh_f(ip,name,user,passwd):
data = time.strftime('%Y-%m-%d')
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=user, password=passwd)
print('成功连接', ip, name,'\n')
cmd_file = open('cmd.txt', 'r')
cmds = cmd_file.readlines()
log = open(r'D:\BaiduNetdiskDownload\test\巡检日志\\' + ip + name + data + '.txt', 'a')
command = ssh_client.invoke_shell()
command.send('terminal length 0\n') # 取消单屏显示
for cmd in cmds:
command.send(cmd+'\n')
time.sleep(1)
output = command.recv(65535)
log.write(output.decode('UTF-8'))
print(ip + '日志载入成功'+'\n')
ssh_client.close()
log.close() def main():
device_authentication_failed_list = []
device_not_reachable_list = []
ip_list,name_list,user_list,passwd_list = device_info()
for ip,name,user,passwd in zip(ip_list,name_list,user_list,passwd_list): # 遍历多个列表可以用zip
try:
a = threading.Thread(target=ssh_f,args=(ip,name,user,passwd))
a.start()
except paramiko.ssh_exception.AuthenticationException:
print(name + '(' + ip + ')' + '身份验证登录失败..')
device_authentication_failed_list.append(ip)
except socket.error:
print(name + '(' + ip + ')' + ' 网络无法访问..')
device_not_reachable_list.append(ip)
time.sleep(5)
print('\n以下设备身份验证登录失败: ')
if device_authentication_failed_list == []:
print('nothing')
else:
for i in device_authentication_failed_list:
print(i)
print('\n网络无法访问以下设备:')
if device_not_reachable_list == []:
print('nothing')
else:
for i in device_not_reachable_list:
print(i) if __name__ == '__main__':
main()

python多线程批量操作交换机的更多相关文章

  1. python多线程学习记录

    1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...

  2. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  3. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  4. python多线程

    python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...

  5. python 多线程就这么简单(转)

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  6. python 多线程就这么简单(续)

    之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...

  7. python多线程监控指定目录

    import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...

  8. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...

  9. 【python,threading】python多线程

    使用多线程的方式 1.  函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...

  10. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

随机推荐

  1. 使用Prometheus和Grafana监控nacos集群

    官方文档:https://nacos.io/zh-cn/docs/monitor-guide.html 按照部署文档搭建好Nacos集群 配置application.properties文件,暴露me ...

  2. Elasticsearch基础但非常有用的功能之二:模板

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484584&idx=1&sn=accfb65 ...

  3. 使用scrapy爬取长安有妖气小说

    目标网站:https://www.snwx3.com/txt/434282.html 第一章地址:https://www.snwx3.com/book/434/434282/92792998.html ...

  4. SQLyog企业版

    一.下载与安装 1.SQLyog的下载 链接:https://www.aliyundrive.com/s/11ZuFqB1z4E提取码:63fu 2.SQLyog的安装 双击下载程序,可以选择默认安装 ...

  5. NOIP2011 提高组 聪明的质监员(二分+前缀和)

    看到这道题,应该都能想到用二分,那问题是怎么去判定呢? 我们考虑用前缀和(a1统计w,a2统计v),枚举每个矿石,,当前判定的值是x,如果该矿石的w>=x,a1[i]=a1[i-1]+1,a2[ ...

  6. 记一次 .NET 某工控视觉软件 非托管泄漏分析

    一:背景 1.讲故事 最近分享了好几篇关于 非托管内存泄漏 的文章,有时候就是这么神奇,来求助的都是这类型的dump,一饮一啄,莫非前定.让我被迫加深对 NT堆, 页堆 的理解,这一篇就给大家再带来一 ...

  7. C# 基础 之:Task 用法

    参考来源:https://www.cnblogs.com/zhaoshujie/p/11082753.html 他介绍的可以说是非常详细,附带Demo例子讲解 1.入门 Task看起来像一个Threa ...

  8. .NET 7 RC 2 发布,倒计时一个月发布正式版

    微软2022-10-22 发布了 .NET 7 RC 2,下一站是.NET 7正式发布,就在下个月Net Conf 2022(11月8日)期间正式发布. 经过长达一年时间的开发,.NET 7 规划的所 ...

  9. spring boot使用swagger生成api接口文档

    前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...

  10. window安装MySQL 5.5教程

    window安装MySQL 5.5教程 1.官网下载MySQL 5.5 下载地址:https://dev.mysql.com/downloads/mysql/5.5.html#downloads 2. ...