fabric实例——批量操作服务器

转自:http://blog.csdn.net/xiangliangyu2008/article/details/8235467

借助fabric模块写了个批量操作服务器的脚本:

准备工作:

安装python2.6.5:

yum -y install readline*

tar xf Python-2.6.5.tar.bz2

cd Python-2.6.5

将目录下Modules/Setup.dist文件中”readline readline.c-lreadline -ltermcap”行前的注释去掉

编译安装:

./configure –enable-shared

make -j8 && make install

安装setuptools

tar xf setuptools-0.6c11.tar.gz

cd setuptools-0.6c11

python setup.py install

安装fabric

执行安装时,软件会自动从网上查找依赖的安装包并进行安装

tar xf fabric-0.9rc2.tar.gz

cd goosemo-fabric-1eacbf2

python setup.py install


[python]  view plain copy
  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3. #####################################################   
  4. # Version: 2.6.5   
  5. # Filename: python_pssh.py   
  6. #####################################################   
  7. from fabric.api import env,run,put,get  
  8. from os import path  
  9. from re import findall   
  10. from sys import argv  
  11. from fabric.context_managers import hide  
  12. from time import sleep  
  13. #USER 登录用户,默认root  
  14. #HOST 单个主机  
  15. #IP_LIST 多个主机所在文件  
  16. #PORT ssh端口,默认22  
  17. #PRI_KEY ssh私钥  
  18. #PASSWORD ssh密码  
  19. #CMD 远程操作命令  
  20. #uSRC 上传的文件  
  21. #uDST 上传目的服务器的目录路径  
  22. #dSRC 下载的文件  
  23. #dDST 下载本地的目录路径  
  24. #time 超时时间,默认1s  
  25. USER='root'  
  26. HOST,IP_LIST=[],[]  
  27. PORT='22'  
  28. PRI_KEY,PASSWORD,CMD,uSRC,uDST,dSRC,dDST= "","","","","","",""  
  29. timeout=1  
  30. for i in range(1,len(argv)+1):  
  31.     if argv[i-1] == '-h' or len(argv)==1:  
  32.             print """ 
  33.             USAGE: 
  34.        -u [user]       Use this argument to specify the user,default is 'root' 
  35.        -H [host]       The host that you want to connect 
  36.        -f [file]       The file content multiple ip address you want to connect 
  37.        -P [port]       The ssh port,default is 22 
  38.        -p [pwd|file]   You can specify password or a priviate key file to connect the host 
  39.        -c [command]    The command you want the host(s) to run 
  40.        -U [src,dst]    The local file that you want to upload to the remote host(s) 
  41.        -D [src,dst]    The remote file that you want to download to the local host 
  42.        -t [timeout]    The program running timeout,default is 1(s) 
  43.        -h              Print this help screen 
  44.             """  
  45.             #选项是第i-1个,那么值就是第i个  
  46.     if argv[i-1] == '-u':  
  47.             USER=argv[i]  
  48.             env.user='%s'%(USER)  
  49.     else:  
  50.             env.user='%s'%(USER)  
  51.     if argv[i-1] == '-H':  
  52.         #正则找出ip,存入HOST数组  
  53.         arg=findall('(\d+\.\d+\.\d+\.\d+|\s+\.{3,4})',argv[i])  
  54.         for j in arg:  
  55.             #判断是否是有类型,str  
  56.             if type(j).__name__ !='NoneType':  
  57.                 HOST.append(j)  
  58.             else:  
  59.                 print 'The HostIP input error'  
  60.                 sys.exit(1)    
  61.     if argv[i-1] == '-P':  
  62.         PORT=argv[i]  
  63.     if argv[i-1] == '-f':  
  64.         if path.isfile('%s'%(argv[i])) == True:  
  65.             IP_LIST=open('%s'%(argv[i]),'r').readlines()  
  66.     if argv[i-1] == '-p':  
  67.         if path.isfile(argv[i]) == True:  
  68.             PRI_KEY=argv[i]  
  69.             env.key_filename='%s'%(PRI_KEY)  
  70.         else:  
  71.             PASSWORD=argv[i]  
  72.             env.password='%s'%(PASSWORD)  
  73.     if argv[i-1] == '-c':  
  74.         CMD=argv[i]  
  75.     if argv[i-1]=='-t':                                                                                         
  76.         timeout=argv[i]    
  77.     SLP='sleep %s'%(timeout)  
  78.     if argv[i-1] == '-U':  
  79.         x=src=argv[i].split(',')  
  80.         uSRC=x[0]  
  81.         uDST=x[1]  
  82.     if argv[i-1] == '-D':  
  83.         y=src=argv[i].split(',')  
  84.         dSRC=y[0]  
  85.         dDST=y[1]             
  86. #   else:  
  87.     IP_PORT=[]  
  88.     if len(IP_LIST)!=0:  
  89.         for k in IP_LIST:  
  90.             IP_PORT.append(k.strip()+':'+PORT)  
  91.     if len(HOST)!=0:  
  92.         for k in HOST:  
  93.             IP_PORT.append(k.strip()+':'+PORT)  
  94.               
  95.               
  96. #执行CMD  
  97. if CMD != "":  
  98.     def command():  
  99.             with hide('running'):  
  100.                 run("%s;%s" %(CMD,SLP))  
  101.     for ip in IP_PORT:  
  102.         env.host_string=ip  
  103.         print "Execute command : \"%s\" at Host : %s" %(CMD,ip.split(':')[0])  
  104.         print "-----------------"  
  105.         command()  
  106.         print "-----------------"  
  107.   
  108. #上传  
  109. if uSRC and uDST != "":  
  110.     def upload():  
  111.         with hide('running'):  
  112.             put("%s" %(uSRC),"%s" %(uDST))  
  113.     for ip in IP_PORT:  
  114.             env.host_string=ip  
  115.             print "Upload local file : \"%s\" to Host : %s \"%s\"" %(uSRC,ip.split(':')[0],uDST)  
  116.             print "-----------------"  
  117.             upload()  
  118.             print "-----------------"  
  119.   
  120. #下载              
  121. if dSRC and dDST != "":  
  122.     def download():  
  123.         with hide('running'):  
  124.             get("%s" %(dSRC),"%s" %(dDST))  
  125.     for ip in IP_PORT:  
  126.         env.host_string=ip  
  127.             print "Download remote file : \"%s\" from Host : %s to local \"%s\"" %(dSRC,ip.split(':')[0],dDST)  
  128.             print "-----------------"  
  129.             download()  
  130.             print "-----------------"  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值