日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python2.7多线程的批量操作远程服务器

發(fā)布時(shí)間:2024/9/5 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python2.7多线程的批量操作远程服务器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#!/usr/bin/env python #-*- coding:utf-8 -*- #多線程批量遠(yuǎn)程執(zhí)行一條命令 # made in chinaimport threading import sys,os import subprocess#cmd = sys.argv[2] ip_list=[] ip_fail=[]def subprocess_caller(cmd):try:p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)output, error = p.communicate()except OSError, e:print 'SUBPROCEEE_CALLER function: execute command failed, message is %s' % ereturn dict(output = [], error = [], code = 1)else:return dict(output = output, error = error, code = 0)#cmd def run_cmd(num,ip):cmd = sys.argv[2]tmp_cmd = "ssh -o ConnectTimeout=3 -o StrictHostKeyChecking=no root@%s 2>/dev/null \"%s\" "%(ip,cmd.strip())res=subprocess_caller(tmp_cmd)#print "===================="#print "The %s is %s success"%(ip,num)#print "===================="if res['output'] != '':print "\033[1;35m The %s is succeed \033[0m \n %s \n"%(ip,res['output'])else:print "\033[1;31m The %s is failed \040"%(ip)ip_fail.append(ip)#ip def get_ip():ip_file = sys.argv[1]input = open(ip_file,'r')while True:tmp_ip = input.readline()if tmp_ip != '':#print type(tmp_ip)ip_list.append(tmp_ip.strip())else:breakinput.close()#print ip_list#多線程 def main():get_ip()threads = []nloops = range(len(ip_list))for i in nloops:t = threading.Thread(target=run_cmd,args=(i, ip_list[i]))threads.append(t)for i in nloops:threads[i].start()for i in nloops:threads[i].join()if len(ip_fail) != 0: print ip_failif __name__ == '__main__':main()

  

轉(zhuǎn)載于:https://www.cnblogs.com/bill2014/p/8336437.html

總結(jié)

以上是生活随笔為你收集整理的python2.7多线程的批量操作远程服务器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。