Python 简单的多线程执行命令
2013-11-07 14:49
471 查看
Tools.py
脚本读取两个参数,第一个为存放IP的文本,第二个为shell命令
效果如下:
够简单的哈。。。直接调用ssh。。
#!/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] f = 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) s = threading.Semaphore(threading_num) for line in list: ip = line.strip() t = threading.Thread(target=execute,args=(s, ip,cmd,log_path_today)) t.setDaemon(True) t.start() main_thread = threading.currentThread() for t in threading.enumerate(): if t 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命令
效果如下:
够简单的哈。。。直接调用ssh。。
相关文章推荐
- 利用python多线程执行远程linux上命令
- 利用python多线程执行远程linux上命令
- python 多线程远程执行命令
- python实现多线程的方式及多条命令并发执行
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- 实用的 Python —— os.system() 在 python 语句中执行 dos 命令
- python 执行命令 获取返回值 和 print内容 用module commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- python 执行系统命令的方法
- 用python SSH模块登录,并在远程机执行shell命令
- Python多线程就这么简单
- linux .sh文件,简单命令执行重启scrapy 程序(批量kill pid、后台运行程序)
- 利用python的paramiko模块ssh登录本地虚拟机并执行任意命令
- Python实现SSH远程登陆,并执行远程命令
- Python中执行系统命令常见的几种方法
- Python执行系统命令的方法 os.system(),os.popen(),commands
- 使用cocos studio打包apk报错或者用python打包出错,执行命令出错,返回值:
- python使用os.system执行Linux shell命令
- python 多线程就这么简单(续)