python2.7多线程的批量操作远程服务器
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#多线程批量远程执行一条命令
# made in china import 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' % e
return 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:
break input.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_fail if __name__ == '__main__':
main()
python2.7多线程的批量操作远程服务器的更多相关文章
- python2.7实现websocket服务器,可以在web实时显示远程服务器日志
一.开始的话 使用python实现websocket服务器,可以在浏览器上实时显示远程服务器的日志. 之前写了一个发布系统,每次发布版本后,为了了解发布情况(进度.是否有错误)都会登录到服务器上查看日 ...
- [Xcode 实际操作]八、网络与多线程-(17)使用网址会话对象URLSession向远程服务器上传图片
目录:[Swift]Xcode实际操作 本文将演示如何通过网址会话对象URLSession向远程服务器上传图片. 网址会话对象URLSession具有在后台上传和下载.暂停和恢复网络操作.丰富的代理模 ...
- python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)
-*- paramiko.util.log_to_file( ssh = paramiko.SSHClient() ssh.set_missing ...
- python批量操作Linux服务器脚本,key登录(执行命令、上传、下载)(二)
-*- 2 #批量操作linux服务器(执行命令,上传,下载) 3 #!/usr/bin/python 4 import paramiko 5 import datetime ...
- python websocket网页实时显示远程服务器日志信息
功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...
- Python 实现远程服务器批量执行命令
paramiko 远程控制介绍 Python paramiko是一个相当好用的远程登录模块,采用ssh协议,可以实现linux服务器的ssh远程登录.首先来看一个简单的例子 import parami ...
- 【WCF】如何将WCF部署到远程服务器
一.前言 最近需要将自己写的WCF服务部署到远程服务器上,也就是公网上.宿主是IIS,在配置成功之前遇到了很多问题,问题如下: 1. WCF该怎么宿主在IIS上,为何会出现 400 Bad ...
- scp 从远程服务器上一下载文件
scp -P202 xx3.x6.xx.xx:/usr/local/zookeeper-.zip /tmp #指定远程服务器的端口和远程服务器的目标文件 ,最后指定要下载到本的地目录 也可以从远程服务 ...
- 非域环境下搭建自动故障转移镜像无法将 ALTER DATABASE 命令发送到远程服务器实例的解决办法
非域环境下搭建自动故障转移镜像无法将 ALTER DATABASE 命令发送到远程服务器实例的解决办法 环境:非域环境 因为是自动故障转移,需要加入见证,事务安全模式是,强安全FULL模式 做到最后一 ...
随机推荐
- Extjs不错的博客
http://www.cnblogs.com/fangsui/category/372751.html http://www.cnblogs.com/WangJinYang/tag/EXT.NET/ ...
- Python 文件类型
Python的文件类型分为以下几种: 1. 源代码文件,也就是以 .py 为扩展名的文件,由 python 程序解释,不需要编译 2. 字节代码文件,python 源代码文件经过编译后生成的扩展名为 ...
- ScrollView拉到尽头时出现阴影的解决方法
<code class="hljs markdown has-numbering" style="display: block; padding: 0px; col ...
- OnGlobalLayoutListener用法
1.implements ViewTreeObserver.OnGlobalLayoutListener{} 2.mContentView.getViewTreeObserver().addOnGlo ...
- C++中的字节对齐分析
struct A { int a; char b; short c; }; struct B { char a; int b; short c; }; #pragma pack(2) struct C ...
- linux IP 设置
修改ip地址即时生效:# ifconfig eth0 192.168.1.102 netmask 255.255.255.0启动生效:修改/etc/sysconfig/network-scripts/ ...
- php数据访问之查询关键字
本文根据数据库中的car表做一个汽车查询页面,巩固php查询关键字操作,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下 一.一个关键字查询 主 ...
- 微软雅黑的Unicode码和英文名
中文名 英文名 Unicode编码 微软雅黑 Microsoft YaHei \5FAE\8F6F\96C5\9ED1 宋 体 SimSun \5B8B\4F53 黑 体 SimHei \9ED1\4 ...
- Windows Phone 自定义一个启动画面
1.新建一个UserControl <UserControl x:Class="LoadingPage.PopupSplash" xmlns="http://sch ...
- Hive JOIN使用详解
转自http://shiyanjun.cn/archives/588.html Hive是基于Hadoop平台的,它提供了类似SQL一样的查询语言HQL.有了Hive,如果使用过SQL语言,并且不理解 ...