zoukankan      html  css  js  c++  java
  • 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,'
    ',HOSTPW
     47                 IP_LIST.append(HOSTIP)
     48                 env.password ='%s'%HOSTPW
     49                 #print IP_LIST,'
    ',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 : 33[1;33;40m %s33[0m  at Host : 33[1;33;40m %s 33[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:33[1;33;40m %s 33[0m to remote HOST:33[1;33;40m %s33[0m : 33[1;33;40m %s33[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:33[1;33;40m %s 33[0m from host :33[1;33;40m %s33[0m to local 33[1;33;40m %s33[0m" %(getSRC,ipport.split(':')[0],getDST)
    103         print "*****************************************************************"
    104         GETdown()
    105         print "*****************************************************************"
  • 相关阅读:
    sass接触
    css 文字超出部分显示省略号(原)
    vue组件
    字节流
    File类、递归
    异常
    静态导入、可变参数、Collections集合工具类、集合嵌套
    Map接口
    Set接口
    List接口
  • 原文地址:https://www.cnblogs.com/shantu/p/4598945.html
Copyright © 2011-2022 走看看