码迷,mamicode.com
首页 > 编程语言 > 详细

python多线程运维脚本

时间:2015-01-24 15:58:24      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:python   多线程   并发   

需求,有一个IP列表文件 ip.txt,里面有1000个ip,那么我要用python同时来处理这1000个IP。
先看ip.txt
192.168.1.1
192.168.1.2
192.168.1.3
......
192.168.1.1000


多线程并发脚本

#!/usr/bin/python
import threading
import sys
import os
import time

def ssh_cmd(ip):        //定义一个ssh_cmd函数 用于发呆5秒,输出ip
        time.sleep(5)
        print ip

def ssh_cmd_spit(list):        //定义一个ssh_cmd_spit函数,用于执行分割后的ip列表
        for j in list:
                j = j.strip("\n")
                ssh_cmd(j)

def thread_main(count):        //定义一个thread_main函数,用于设置每个进程处理的IP个数,设置为1,那么1000个IP需要同时开1000个线程,设置为50,那么需要20个线程来同时处理。
        file = open("ip.txt")
        f = file.readlines()
        for i in range(0,len(f),int(count)):
                b = f[i:i+count]
                t = threading.Thread(target=ssh_cmd_spit,args=(b,))   //添加线程
                t.start()        //处理线程

if __name__ == '__main__':
        thread_main(1)


python多线程运维脚本

标签:python   多线程   并发   

原文地址:http://blog.csdn.net/apache0554/article/details/43085187

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!