python 遠程執行命令、發布檔案

來源:互聯網
上載者:User

標籤:

 最近有個需求,想擷取部分伺服器上運行了那些應用服務,一台台去看,太費勁了,參考牛人寫了此指令碼,後期再加上一個前端頁面做一些簡單的營運維護的工作,湊合著先用著,待完善,註:此指令碼依懶於安裝fabric ,安裝過程參考: http://5973819.blog.51cto.com/5963819/1532334
  1 #!/usr/bin/env python  2 #coding:utf8  3 #####################################################   4 # Author: wangganyu188 [email protected]  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 = ipport102         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 "*****************************************************************"

 

python 遠程執行命令、發布檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.