本帖最后由 Matthew0701 于 2011-4-28 21:45 编辑
最近在学习python,借助fabric模块写了个批量操作服务器的脚本,在此分享给大家,如有不足之处,欢迎大家指正
准备工作:
安装python 2.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
############################################################
脚本执行效果:
显示帮助:
[root@testA python]# python pyssh.py
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
在多个远程主机上执行shell命令:
[root@testA python]# python pyssh.py -H 10.0.101.241,10.0.101.243 -p 123456 -P 22 -c "ps -ef | awk '{print \$NF}'|wc -l"
Execute command : "ps -ef | awk '{print $NF}'|wc -l" at Host : 10.0.101.241
-------------------------------------------------
83
-------------------------------------------------
Execute command : "ps -ef | awk '{print $NF}'|wc -l" at Host : 10.0.101.243
-------------------------------------------------
76
-------------------------------------------------
[root@testA python]# cat ip.txt
10.0.101.241
10.0.101.243
[root@testA python]# python pyssh.py -f ip.txt -p 123456 -P 22 -c "ps -ef | awk '{print \$NF}'|wc -l"
Execute command : "ps -ef | awk '{print $NF}'|wc -l" at Host : 10.0.101.241
-------------------------------------------------
82
-------------------------------------------------
Execute command : "ps -ef | awk '{print $NF}'|wc -l" at Host : 10.0.101.243
-------------------------------------------------
77
-------------------------------------------------
将远程主机上的文件下载到本地目录:
[root@testA python]# python pyssh.py -H 10.0.101.243 -p 123456 -P 22 -D '/pyssh.py,/'
Download remote file : "/pyssh.py" from Host : 10.0.101.243 to local "/"
-------------------------------------------------
-------------------------------------------------
将本地文件上传到多个远程主机上
[root@testA python]# python pyssh.py -H 10.0.101.241,10.0.101.242 -p 123456 -P 22 -U 'pyssh.py,/'
Upload local file : "pyssh.py" to Host : 10.0.101.241 "/"
-------------------------------------------------
-------------------------------------------------
Upload local file : "pyssh.py" to Host : 10.0.101.242 "/"
-------------------------------------------------
pyssh.zip
(1.38 KB, 下载次数: 359)
2011-4-28 12:38:18 上传
下载次数: 359
pssh脚本
2011-4-28 13:38:34 上传
下载次数: 242
fabric-0.9rc2.tar.gz
(146.3 KB, 下载次数: 226)
2011-4-28 13:38:34 上传
下载次数: 226