借助fabric模块写了个批量操作服务器的脚本:
准备工作:
安装python2.6.5:
yum -y install readline*
tar xf Python-2.6.5.tar.bz2
cd Python-2.6.5
将目录下Modules/Setup.dist文件中”readline readline.c-lreadline -ltermcap”行前的注释去掉
编译安装:
./configure –enable-shared
make -j8 && make install
安装setuptools
tar xf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py install
安装fabric
执行安装时,软件会自动从网上查找依赖的安装包并进行安装
tar xf fabric-0.9rc2.tar.gz
cd goosemo-fabric-1eacbf2
python setup.py install
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#####################################################
# Version: 2.6.5
# Filename: python_pssh.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 ssh端口,默认22
#PRI_KEY ssh私钥
#PASSWORD ssh密码
#CMD 远程操作命令
#uSRC 上传的文件
#uDST 上传目的服务器的目录路径
#dSRC 下载的文件
#dDST 下载本地的目录路径
#time 超时时间,默认1s
USER='root'
HOST,IP_LIST=[],[]
PORT='22'
PRI_KEY,PASSWORD,CMD,uSRC,uDST,dSRC,dDST= "","","","","","",""
timeout=1
for i in range(1,len(argv)+1):
if argv[i-1] == '-h' or len(argv)==1:
print """
USAGE:
-u [user] Use this argument to specify the user,default is 'root'
-H [host] The host that you want to connect
-f [file] The file content multiple ip address you want to connect
-P [port] The ssh port,default is 22
-p [pwd|file] You can specify password or a priviate key file to connect the host
-c [command] The command you want the host(s) to run
-U [src,dst] The local file that you want to upload to the remote host(s)
-D [src,dst] The remote file that you want to download to the local host
-t [timeout] The program running timeout,default is 1(s)
-h Print this help screen
"""
#选项是第i-1个,那么值就是第i个
if argv[i-1] == '-u':
USER=argv[i]
env.user='%s'%(USER)
else:
env.user='%s'%(USER)
if argv[i-1] == '-H':
#正则找出ip,存入HOST数组
arg=findall('(\d+\.\d+\.\d+\.\d+|\s+\.{3,4})',argv[i])
for j in arg:
#判断是否是有类型,str
if type(j).__name__ !='NoneType':
HOST.append(j)
else:
print 'The HostIP input error'
sys.exit(1)
if argv[i-1] == '-P':
PORT=argv[i]
if argv[i-1] == '-f':
if path.isfile('%s'%(argv[i])) == True:
IP_LIST=open('%s'%(argv[i]),'r').readlines()
if argv[i-1] == '-p':
if path.isfile(argv[i]) == True:
PRI_KEY=argv[i]
env.key_filename='%s'%(PRI_KEY)
else:
PASSWORD=argv[i]
env.password='%s'%(PASSWORD)
if argv[i-1] == '-c':
CMD=argv[i]
if argv[i-1]=='-t':
timeout=argv[i]
SLP='sleep %s'%(timeout)
if argv[i-1] == '-U':
x=src=argv[i].split(',')
uSRC=x[0]
uDST=x[1]
if argv[i-1] == '-D':
y=src=argv[i].split(',')
dSRC=y[0]
dDST=y[1]
# else:
IP_PORT=[]
if len(IP_LIST)!=0:
for k in IP_LIST:
IP_PORT.append(k.strip()+':'+PORT)
if len(HOST)!=0:
for k in HOST:
IP_PORT.append(k.strip()+':'+PORT)
#执行CMD
if CMD != "":
def command():
with hide('running'):
run("%s;%s" %(CMD,SLP))
for ip in IP_PORT:
env.host_string=ip
print "Execute command : \"%s\" at Host : %s" %(CMD,ip.split(':')[0])
print "-----------------"
command()
print "-----------------"
#上传
if uSRC and uDST != "":
def upload():
with hide('running'):
put("%s" %(uSRC),"%s" %(uDST))
for ip in IP_PORT:
env.host_string=ip
print "Upload local file : \"%s\" to Host : %s \"%s\"" %(uSRC,ip.split(':')[0],uDST)
print "-----------------"
upload()
print "-----------------"
#下载
if dSRC and dDST != "":
def download():
with hide('running'):
get("%s" %(dSRC),"%s" %(dDST))
for ip in IP_PORT:
env.host_string=ip
print "Download remote file : \"%s\" from Host : %s to local \"%s\"" %(dSRC,ip.split(':')[0],dDST)
print "-----------------"
download()
print "-----------------"