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 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...
随机推荐
- 记一次TIME_WAIT网络故障
文章转载自:https://blog.51cto.com/dngood/988968
- 关于aws cli命令的exit/return code分析
最近总是收到一个备份脚本的失败邮件,脚本是之前同事写的,没有加入任何有调试信息,及有用的日志 于是去分析 ,脚本中有一条 aws s3 sync $srclocal $dsts3 命令,然后根据这条 ...
- Codeforces Round #708 (Div. 2)
A题被hack,A题很简单,其实题目没看懂,直接看样例做的. B题题意是以为懂了,但是样例一直看不懂. 经验:要两两相加能被一个m整除数组sum最少,利用他们的余数就可以设为a[x], x是余数,如果 ...
- P3008 [USACO11JAN]Roads and Planes G (最短路+拓扑排序)
该最短路可不同于平时简单的最短路模板. 这道题一看就知道用SPFA,但是众所周知,USACO要卡spfa,所以要用更快的算法. 单向边不构成环,双向边都是非负的,所以可以将图分成若干个连通块(内部只有 ...
- esp32把玩记-④ 星星点灯 (点亮led)
注意 全程使用Micropython,不会安装看我第一篇文章感谢 正式开始 用Thonny烧录(运行)以下代码 import time from machine import Pin led=Pin( ...
- XSS-Game
很简单的弹窗 http://192.168.31.177/xssgame/level1.php?name=<script>alert(1)</script> 过滤了>.& ...
- 前端框架Vue------>第一天学习(2) v-if
API:https://cn.vuejs.org/v2/api/#key 文章目录 5.条件渲染 5.1 . v-if 5.2 . v-else-if 6 .列表渲染 7 .事件监听 5.条件渲染 5 ...
- 抛砖系列之redis监控命令
前言 redis是一款非常流行的kv数据库,以高性能著称,其高吞吐.低延迟等特性让广大开发者趋之若鹜,每每看到别人发出的redis故障报告都让我产生一种居安思危,以史为鉴的危机感,恰逢今年十一西安烟雨 ...
- python的一些运算符
# 1.算术运算符 print('1.算术运算符') # 1.1 + 求和 a = 10 b = 20 c = a + b print(c) print('a+b={}'.format(c)) pri ...
- ARM TrustZone白皮书部分阅读
嵌入式系统安全的一些解决方法及缺陷 外部硬件安全模块:在主SoC之外包含一个专用的硬件安全模块或可信元件,e.g. 手机的SIM卡.隔离仅限于可以从非易失性存储器运行的相对静态程序 内部硬件安全模块: ...