python 遠程執行命令、發佈文件

      最近有個需求,想獲取部分服務器上運行了那些應用服務,一臺臺去看,太費勁了,參考牛人寫了此腳本,後期再加上一個前端頁面做一些簡單的運維維護的工作,湊合着先用着,待完善,

注:此腳本依懶於安裝fabric ,安裝過程參考: http://5973819.blog.51cto.com/5963819/1532334


#!/usr/bin/env python

#coding:utf8

##################################################### 

# Author: wangganyu188 [email protected]

# 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='22'

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 "*****************************************************************"


使用示例如下:

幫助:

wKioL1QJPDzx_ooIAAHHs09G_kM956.jpg

遠程執行命令

wKiom1QJO7HzxjA-AAKuG5BNpJY393.jpg

上傳文件:

wKioL1QJPJCDQoYkAAHunkuIrK0963.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章