python多线程批量操作交换机
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多线程批量操作交换机的更多相关文章
- python多线程学习记录
1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...
- python多线程编程
Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- python多线程
python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- python 多线程就这么简单(续)
之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...
- python多线程监控指定目录
import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...
- python多线程ssh爆破
python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...
- 【python,threading】python多线程
使用多线程的方式 1. 函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...
- <转>Python 多线程的单cpu与cpu上的多线程的区别
你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...
随机推荐
- 第六章:Django 综合篇 - 4:django-admin和manage.py
目录 一.Django内置命令选项 check dbshell diffsettings flush makemigrations migrate runserver shell startapp s ...
- 使用shell脚本定时重启tomcat服务
#!/bin/bash DATE=`date +%Y-%m-%d-%H-%M-%S` echo "当前时间是:$DATE" # 根据端口号查找进程 PID=`/usr/sbin/l ...
- 自定义mapping与常见参数
PUT test { "mappings": { "dynamic": true, "properties": { "firstn ...
- 在 CentOS8/RHEL8 中配置 Rsyslog 服务器
Rsyslog 是一个自由开源的日志记录程序,在 CentOS 8 和 RHEL 8 系统上默认可用.它提供了一种从客户端节点到单个中央服务器的"集中日志"的简单有效的方法.日志集 ...
- NSIS使用API创建工具提示条和超级链接
不再借助专用插件创建超级链接和工具提示条 !includensDialogs.nsh #编写:水晶石 Name "link_tooltips" OutFile "link ...
- Leetcode链表
Leetcode链表 一.闲聊 边学边刷的--慢慢写慢慢更 二.题目 1.移除链表元素 题干: 思路: 删除链表节点,就多了一个判断等值. 由于是单向链表,所以要删除节点时要找到目标节点的上一个节点, ...
- Java程序设计(四)作业
要求:定义一个Java项目,项目名为"学号_姓名_题号",如:"20181101_张三_1",完成后将项目复制到桌面并压缩提交到邮箱82794085@qq.co ...
- 华为路由器vrrp(虚拟路由器冗余协议)基本配置命令
vrrp(虚拟路由器冗余协议)基本配置 int g0/0/0 vrrp vrid 1 virtual-ip 172.16.1.254 创建VRRP备份组,备份组号为1,配置虚拟IP为172.16.1. ...
- JVM中的方法区
JVM中的方法区 方法区存储什么? 用于存储已被虚拟机加载的类型信息.常量.静态变量.即时编译器编译后的代码缓存 1.类型信息 对每个加载的类型(类class.接口interface.枚举.注解)jv ...
- Vue学习之--------Vue中过滤器(filters)的使用(代码实现)(2022/7/18)
1.过滤器 1.1 概念 过滤器: 定义:对要显示的数据进行特定格式化后再显示(适用于一些简单逻辑的处理). 语法: 1.注册过滤器:Vue.filter(name,callback) 或 new V ...