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

 最近有个需求,想获取部分服务器上运行了那些应用服务,一台台去看,太费劲了,参考牛人写了此脚本,后期再加上一个前端页面做一些简单的运维维护的工作,凑合着先用着,待完善,
注:此脚本依懒于安装fabric ,安装过程参考: http://5973819.blog.51cto.com/5963819/1532334
 #!/usr/bin/env python
#coding:utf8
#####################################################
# Author: wangganyu188 wangganyu188@gmail.com
# Last modified: 2014-09-05
# Filename: sys_ops.py
##################################################### from fabric.api import env,run,put,get
from os import path
from re import findall
from sys import argv
from fabric.context_managers import hide
from time import sleep USER='root'
HOST,IP_LIST=[],[]
PORT=''
timeout = 1
CMD,getSRC,getDST,putSRC,putDST = '','','','','' for i in range(1,len(argv)+1):
#print i
if argv[i-1] == '-h' or len(argv) == 1:
print """
USAGE:
-c [cmd] The command you want the host(s) to run
-f [file] The file content multiple ip address you want to connect
-P [put] The local file that you want to upload to the remote host(s)
-G [get] The remote file that you want to download to the local host
-h [help] Print this help screen
""" if argv[i-1]=='-f':
if path.isfile('%s'%(argv[i])) == True:
file_list = open('%s'%(argv[i]),'r').readlines()
for line in file_list:
#print line
HOSTIP = line.split()[0]
HOSTPW = line.split()[1]
#print HOSTIP,'\n',HOSTPW
IP_LIST.append(HOSTIP)
env.password ='%s'%HOSTPW
#print IP_LIST,'\n',env.password
if argv[i-1] == '-c':
CMD = argv[i] if argv[i-1] == '-P':
p = src = argv[i].split(',')
putSRC = p[0]
putDST = p[1] if argv[i-1] == '-G':
g = src = argv[i].split(',')
getSRC = g[0]
getDST = g[1] else:
IP_PORT = []
if len(IP_LIST) != 0:
for ip in IP_LIST:
IP_PORT.append(ip + ':' + PORT) if CMD !='':
def command():
with hide('running'):
run("%s"%CMD) for ipport in IP_PORT:
env.host_string = ipport
print "Execute Command : \033[1;33;40m %s\033[0m at Host : \033[1;33;40m %s \033[0m" %(CMD,ipport.split(':')[0])
print "***************************************************************"
command()
print '***************************************************************' if putSRC and putDST != '':
def PUTupload():
with hide('running'):
put("%s"%(putSRC),"%s"%(putDST))
for ipport in IP_PORT:
env.host_string = ipport
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)
print "*****************************************************************"
PUTupload()
print "*****************************************************************" if getSRC and getDST != '':
def GETdown():
with hide('running'):
get("%s"%(getSRC),"%s"%(getDST))
for ipport in IP_PORT:
env.host_string = ipport
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)
print "*****************************************************************"
GETdown()
print "*****************************************************************"
上一篇:python 远程执行命令


下一篇:BlazeMeter发布chrome扩展插件,支持JMeter脚本创建