Python 简单的多线程执行命令

简介:

Tools.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
#coding:utf-8
import  threading
import  subprocess
import  os
import  sys
sshport  =  13131
log_path  =  'update_log'
output  =  {}
def  execute(s, ip, cmd, log_path_today):
     with s:     
         cmd  =  '''ssh -p%s root@%s -n "%s" '''  %  (sshport, ip, cmd)
         ret  =  subprocess.Popen(cmd, shell = True , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
         output[ip]  =  ret.stdout.readlines()
if  __name__  = =  "__main__" :
     if  len (sys.argv) ! =  3 :
         print  "Usage: %s config.ini cmd"  %  sys.argv[ 0 ]
         sys.exit( 1 )
                                      
     if  not  os.path.isfile(sys.argv[ 1 ]):
         print  "Usage: %s is not file!"  %  sys.argv[ 1 ]
         sys.exit( 1 )
                                          
     cmd  =  sys.argv[ 2 ]
                                      
     =  open (sys.argv[ 1 ], 'r' )
     list  =  f.readlines()
     f.close()
     today  =  datetime.date.today()
     log_path_today  =  '%s/%s'  %  (log_path,today)
     if  not  os.path.isdir(log_path_today):
         os.makedirs(log_path_today)
                                      
     threading_num  =  100
     if  threading_num >  len ( list ):
         threading_num  =  len ( list )
     =  threading.Semaphore(threading_num)
                                      
     for  line  in  list :
         ip  =  line.strip()
         =  threading.Thread(target = execute,args = (s, ip,cmd,log_path_today))
         t.setDaemon( True )
         t.start()
                                          
     main_thread  =  threading.currentThread()
     for  in  threading. enumerate ():
         if  is  main_thread:
             continue
         t.join()
                                          
     for  ip,result  in  output.items():
         print  "%s: "  %  ip
         for  line  in  result:
             print  "    %s"  %  line.strip()
                                      
     print  "Done!"


脚本读取两个参数,第一个为存放IP的文本,第二个为shell命令

效果如下:144632398.png


够简单的哈。。。直接调用ssh。。

本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1321655如需转载请自行联系原作者


lihuipeng

相关文章
|
9月前
|
数据采集 存储 JSON
Python爬取知乎评论:多线程与异步爬虫的性能优化
Python爬取知乎评论:多线程与异步爬虫的性能优化
|
9月前
|
人工智能 安全 调度
Python并发编程之线程同步详解
并发编程在Python中至关重要,线程同步确保多线程程序正确运行。本文详解线程同步机制,包括互斥锁、信号量、事件、条件变量和队列,探讨全局解释器锁(GIL)的影响及解决线程同步问题的最佳实践,如避免全局变量、使用线程安全数据结构、精细化锁的使用等。通过示例代码帮助开发者理解并提升多线程程序的性能与可靠性。
290 0
|
6月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
671 0
|
8月前
|
数据采集 消息中间件 并行计算
Python多线程与多进程性能对比:从原理到实战的深度解析
在Python编程中,多线程与多进程是提升并发性能的关键手段。本文通过实验数据、代码示例和通俗比喻,深入解析两者在不同任务类型下的性能表现,帮助开发者科学选择并发策略,优化程序效率。
657 1
|
10月前
|
JSON 算法 Java
打造终端里的下载利器:Python实现可恢复式多线程下载器
在数字时代,大文件下载已成为日常需求。本文教你用Python打造专业级下载器,支持断点续传、多线程加速、速度限制等功能,显著提升终端下载体验。内容涵盖智能续传、多线程分块下载、限速控制及Rich库构建现代终端界面,助你从零构建高效下载工具。
640 1
|
9月前
|
数据采集 监控 调度
干货分享“用 多线程 爬取数据”:单线程 + 协程的效率反超 3 倍,这才是 Python 异步的正确打开方式
在 Python 爬虫中,多线程因 GIL 和切换开销效率低下,而协程通过用户态调度实现高并发,大幅提升爬取效率。本文详解协程原理、实战对比多线程性能,并提供最佳实践,助你掌握异步爬虫核心技术。
|
9月前
|
数据采集 存储 Java
多线程Python爬虫:加速大规模学术文献采集
多线程Python爬虫:加速大规模学术文献采集
|
7月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1124 102
|
7月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
446 104
|
7月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
350 103

推荐镜像

更多