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

相关文章
|
15天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
25天前
|
算法 数据处理 Python
Python并发编程:解密异步IO与多线程
本文将深入探讨Python中的并发编程技术,重点介绍异步IO和多线程两种常见的并发模型。通过对比它们的特点、适用场景和实现方式,帮助读者更好地理解并发编程的核心概念,并掌握在不同场景下选择合适的并发模型的方法。
|
1月前
|
安全 Python
Python中的并发编程:多线程与多进程技术探究
本文将深入探讨Python中的并发编程技术,重点介绍多线程和多进程两种并发处理方式的原理、应用场景及优缺点,并结合实例分析如何在Python中实现并发编程,以提高程序的性能和效率。
|
2天前
|
Java 数据库连接 数据处理
Python从入门到精通:3.1.2多线程与多进程编程
Python从入门到精通:3.1.2多线程与多进程编程
|
8天前
|
调度 Python
Python多线程、多进程与协程面试题解析
【4月更文挑战第14天】Python并发编程涉及多线程、多进程和协程。面试中,对这些概念的理解和应用是评估候选人的重要标准。本文介绍了它们的基础知识、常见问题和应对策略。多线程在同一进程中并发执行,多进程通过进程间通信实现并发,协程则使用`asyncio`进行轻量级线程控制。面试常遇到的问题包括并发并行混淆、GIL影响多线程性能、进程间通信不当和协程异步IO理解不清。要掌握并发模型,需明确其适用场景,理解GIL、进程间通信和协程调度机制。
28 0
|
24天前
|
数据采集 Java API
python并发编程: Python使用线程池在Web服务中实现加速
python并发编程: Python使用线程池在Web服务中实现加速
18 3
python并发编程: Python使用线程池在Web服务中实现加速
|
27天前
|
Java 测试技术 Python
Python开启线程和线程池的方法
Python开启线程和线程池的方法
17 0
Python开启线程和线程池的方法
|
1月前
|
并行计算 Python
Python中的并发编程:多线程与多进程的比较
在Python编程中,实现并发操作是提升程序性能的重要手段之一。本文将探讨Python中的多线程与多进程两种并发编程方式的优劣及适用场景,帮助读者更好地选择合适的方法来提高程序运行效率。
|
1月前
|
并行计算 安全 Unix
Python教程第8章 | 线程与进程
本章主要讲解了线程与进程的概念,多线程的运用以及Python进程的相关案例学习
36 0
|
1月前
|
分布式计算 并行计算 Java
浅析Python自带的线程池和进程池
浅析Python自带的线程池和进程池
89 0