设为首页 收藏本站
查看: 1160|回复: 1

[经验分享] 原创python多线程批量管理工具batch(不断完善)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-5-12 09:34:27 | 显示全部楼层 |阅读模式
#!/usr/bin/env python
import threading
import time
import paramiko
import os,sys
from ip import ip_list,web_server,ip_msg
from optparse import OptionParser
from ssh_co.cfg.config import host_msg
def opts():
   parser = OptionParser(usage="usage %prog options")
   parser.add_option("-i","--item",
                       dest="item",
                       default="",
                       action="store",
                       )
   parser.add_option("-p","--host",
                       dest="host",
                       default="",
                       action="store",
                       )
   parser.add_option("-f","--file",
                       dest="file",
                       default="",
                       action="store",
                       )
   parser.add_option("-s","--sfile",
                       dest="sfile",
                       default="",
                       action="store",
                       )
   parser.add_option("-d","--dfile",
                       dest="dfile",
                       default="",
                       action="store",
                       )
   parser.add_option("-c","--cmd",
                       dest="cmd",
                       default="",
                       action="store",
                       )
   return parser.parse_args()
class Get_date(object):
   def ssh_cmd(self,number,user,ip,port,cmd):
       for i in xrange(number):
           ssh = paramiko.SSHClient()
           ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
           ssh.connect(ip,port,user,"")
           stdin1, stdout1, stderr1 = ssh.exec_command("hostname")
           stdin2, stdout2, stderr2 = ssh.exec_command(cmd)
           out1 = stdout1.read()
           #in2 = stdin2.read()
           out2 = stdout2.read()
           err2 = stderr2.read()
           print "\033[32;1m%s-%s:\n\033[0m%s %s" %(out1.strip(),ip,out2.strip(),err2.strip())
           print "\033[33;1m-\033[0m"*120
           ssh.close()
   def cmd(self,user,ip,port,cmd):
       for i in xrange(len(ip)):
           a=threading.Thread(target=self.ssh_cmd,args=(1,user,ip[i],port,cmd))
           a.start()
   def ssh_file(self,number,user,ip,port,path_ssh_key,sfile,dfile):
       key = paramiko.DSSKey.from_private_key_file(path_ssh_key)
       for i in xrange(number):
           ssh = paramiko.Transport((ip,port))
           ssh_f=ssh.connect(username = user, pkey = key)
           sftp = paramiko.SFTPClient.from_transport(ssh)
           path=os.path.split(dfile)[0]
           try:
               ssh_result=sftp.put(sfile,dfile)
           except Exception as e:
               for i in xrange(1,len(path.split('/'))+1):
                   try:
                       a=path.split('/')[0:i]
                       new_path='/'.join(a)
                       sftp.mkdir(new_path)
                   except Exception as e:
                       continue
               ssh_result=sftp.put(sfile,dfile)
           print "\033[31;1m%s\033[0m"%(ip)
           print "\033[32;1m%s:---------->%s\n\033[0m%s"%(sfile,dfile,ssh_result)
           print "\033[33;1m-\033[0m"*120
           ssh.close()
   def file(self,user,ip,port,path_ssh_key,sfile,dfile):
       for i in xrange(len(ip)):
           a=threading.Thread(target=self.ssh_file,args=(1,user,ip[i],port,path_ssh_key,sfile,dfile))
           a.start()
def main():
   user=host_msg["user"]
   port=host_msg["port"]
   path_ssh_key=host_msg["ssh_key"]
   get_host_date=Get_date()
   opt,args=opts()
   ssh_item=opt.item
   ssh_file=opt.file
   ssh_sfile=opt.sfile
   ssh_dfile=opt.dfile
   ssh_cmd=opt.cmd
   ssh_ip=opt.host
   if ssh_cmd:
       if ssh_ip:
           if args:
               args.append(ssh_ip)
               get_host_date.cmd(user,args,port,ssh_cmd)
           else:
               get_host_date.cmd(user,[ssh_ip],port,ssh_cmd)
       if ssh_item and ssh_item in ip_msg.keys():
           get_host_date.cmd(user,ip_msg[ssh_item],port,ssh_cmd)
   #print ssh_item,ssh_cmd,args,ssh_file
   if ssh_file:
       if ssh_ip:
           if args:
               args.append(ssh_ip)
               get_host_date.file(user,args,port,path_ssh_key,ssh_file,ssh_file)
           else:
               get_host_date.file(user,[ssh_ip],port,path_ssh_key,ssh_file,ssh_file)
       if ssh_item and ssh_item in ip_msg.keys():
           get_host_date.file(user,ip_msg[ssh_item],port,path_ssh_key,ssh_file,ssh_file)
   if ssh_sfile and ssh_dfile:
       if ssh_ip:
           if args:
               args.append(ssh_ip)
               get_host_date.file(user,args,port,path_ssh_key,ssh_sfile,ssh_dfile)
           else:
               get_host_date.file(user,[ssh_ip],port,path_ssh_key,ssh_sfile,ssh_dfile)
       if ssh_item and ssh_item in ip_msg.keys():
           get_host_date.file(user,ip_msg[ssh_item],port,path_ssh_key,ssh_sfile,ssh_dfile)

if __name__=="__main__":
   main()


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-19036-1-1.html 上篇帖子: python管理mysql 下篇帖子: graphite,python监控网卡流量 管理工具 python 多线程
发表于 2014-5-13 01:52:26 | 显示全部楼层
好吧。。。。。。。。。 这个东西,最近我又加了功能

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表