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. 使用Docker Compose部署SpringCloud项目docker-compose.yml文件示例

    注意各组件之间的依赖关系 microservice-discovery-eureka: image: reg.itmuch.com/microservice-discovery-eureka port ...

  2. 19. Fluentd输入插件:in_http用法详解

    in_http插件允许使用HTTP协议来采集日志事件.这个插件会建立一个支持REST风格的HTTP端点,来接收日志事件请求. 配置示例 <source> @type http port 9 ...

  3. C++自学笔记 头文件 (header file)关于 #include 和.h

    头文件 在C++中定义Definition一个类的时候 要用分别的.h和.cpp文件去定义这个类 .h和.cpp成对出现 类的声明declaration和函数原型放在头文件里(.h) 定义这些函数的结 ...

  4. PTA 乙级解题笔记 1001 害死人不偿命的(3n+1)猜想

    卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n+1) 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 ...

  5. Node.js(一)基本node.js读取删除

    npm init -y(初始化项目) npm uninstall (xxx模块名称)=>(移除模块) var text2=require("./text2"); //解构 c ...

  6. 洛谷P1120 小木棍 (搜索+剪枝)

    搜索的经典题. 我们要求木根的最小长度,就要是木根的数量尽可能多,可以发现木根的长度一定可以整除所有小木棒的总长度,从小到大枚举这个可能的长度,第一次有解的就是答案. 关心的状态:当前正在拼哪根木棍, ...

  7. MatrixOne从入门到实践08——SSB性能测试

    MatrixOne从入门到实践--SSB性能测试 SSB 星型模式基准测试是 OLAP 数据库性能测试的常用场景,通过本篇教程,您可以了解到如何在 MatrixOne 中实现 SSB 测试. 测试环境 ...

  8. CentOS 8.2 对k8s基础环境配置

    一.基础环境配置 1 IP 修改 机器克隆后 IP 修改,使Xshell连接上 [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg- ...

  9. 7.RabbitMQ系列之topic主题交换器

    topic主题交换器它根据在队列绑定的路由键和路由模式通配符匹配将消息路由到队列. 生产者在消息头中添加路由键并将其发送到主题交换器. 收到消息后,exchange尝试将路由键与绑定到它的所有队列的绑 ...

  10. dubbo的一系列配置与搭建

    dubbo新的版本采用前后端分离技术,在github上下载的时候,不仅仅只是一个dubbo-admin 而是将admin包分离为dubbo-admin-ui前端包和dubbo-admin-server ...