python批量上传执行脚本_python 写的批量操作远程主机脚本(命令执行,上传、下载文件)...

本帖最后由 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值