python 远程执行命令、发布文件

 最近有个需求,想获取部分服务器上运行了那些应用服务,一台台去看,太费劲了,参考牛人写了此脚本,后期再加上一个前端页面做一些简单的运维维护的工作,凑合着先用着,待完善,
注:此脚本依懒于安装fabric ,安装过程参考: http://5973819.blog.51cto.com/5963819/1532334
  1 #!/usr/bin/env python
  2 #coding:utf8
  3 ##################################################### 
  4 # Author: wangganyu188 wangganyu188@gmail.com
  5 # Last modified: 2014-09-05
  6 # Filename: sys_ops.py
  7 ##################################################### 
  8 
  9 
 10 
 11 from fabric.api import env,run,put,get
 12 from os import path
 13 from re import findall 
 14 from sys import argv
 15 from fabric.context_managers import hide
 16 from time import sleep
 17 
 18 
 19 
 20 USER='root'
 21 HOST,IP_LIST=[],[]
 22 PORT='22'
 23 timeout = 1
 24 CMD,getSRC,getDST,putSRC,putDST = '','','','',''
 25 
 26 for i in range(1,len(argv)+1):
 27     #print i
 28     if argv[i-1] == '-h' or len(argv) == 1:
 29         print """
 30         USAGE:
 31         -c [cmd] The command you want the host(s) to run
 32         -f [file] The file content multiple ip address you want to connect
 33         -P [put] The local file that you want to upload to the remote host(s)
 34         -G [get] The remote file that you want to download to the local host
 35         -h [help]  Print this help screen
 36         """
 37     
 38     
 39     if argv[i-1]=='-f':
 40         if path.isfile('%s'%(argv[i])) == True:
 41             file_list = open('%s'%(argv[i]),'r').readlines()
 42             for line in file_list:
 43                 #print line
 44                 HOSTIP = line.split()[0]
 45                 HOSTPW = line.split()[1]
 46                 #print HOSTIP,'\n',HOSTPW
 47                 IP_LIST.append(HOSTIP)
 48                 env.password ='%s'%HOSTPW
 49                 #print IP_LIST,'\n',env.password
 50     if argv[i-1] == '-c':
 51         CMD = argv[i]
 52 
 53     if argv[i-1] == '-P':
 54         p = src = argv[i].split(',')
 55         putSRC = p[0]
 56         putDST = p[1]
 57 
 58 
 59     if argv[i-1] == '-G':
 60         g = src = argv[i].split(',')
 61         getSRC = g[0]
 62         getDST = g[1] 
 63 
 64 else:
 65     IP_PORT = []
 66     if len(IP_LIST) != 0:
 67         for ip in IP_LIST:
 68             IP_PORT.append(ip + ':' + PORT)
 69 
 70 
 71 
 72 if CMD !='':
 73     def command():
 74         with hide('running'):
 75             run("%s"%CMD)
 76 
 77     for ipport  in IP_PORT:
 78         env.host_string = ipport
 79         print "Execute Command : \033[1;33;40m %s\033[0m  at Host : \033[1;33;40m %s \033[0m" %(CMD,ipport.split(':')[0])
 80         print "***************************************************************"
 81         command()
 82         print '***************************************************************'
 83 
 84 if putSRC and putDST != '':
 85     def PUTupload():
 86         with hide('running'):
 87             put("%s"%(putSRC),"%s"%(putDST))
 88     for ipport in IP_PORT:
 89         env.host_string = ipport
 90         print "PUT local file:\033[1;33;40m %s \033[0m to remote HOST:\033[1;33;40m %s\033[0m : \033[1;33;40m %s\033[0m" %(putSRC,ipport.split(':')[0],putDST)
 91         print "*****************************************************************"
 92         PUTupload()
 93         print "*****************************************************************"
 94             
 95 
 96 if getSRC and getDST != '':
 97     def GETdown():
 98         with hide('running'):
 99             get("%s"%(getSRC),"%s"%(getDST))
100     for ipport in IP_PORT:
101         env.host_string = ipport
102         print "GET remote file:\033[1;33;40m %s \033[0m from host :\033[1;33;40m %s\033[0m to local \033[1;33;40m %s\033[0m" %(getSRC,ipport.split(':')[0],getDST)
103         print "*****************************************************************"
104         GETdown()
105         print "*****************************************************************"

 

posted @ 2015-06-24 23:27  shantuwqk  阅读(567)  评论(0编辑  收藏  举报