服务器多了,有一个烦恼就是如何批量快速操作一堆服务器。这里我推荐一下经常使用利器pssh。这个工具给我的工作带来了莫大的帮助。
简介
pssh是一款开源的软件,使用python实现。用于批量ssh操作大批量机器。
pssh的项目地址 https://code.google.com/p/parallel-ssh/
安装
在pssh的项目主页找到相应的版本,下载到我们的服务器上,解压后执行python setup.py安装。下面以pssh-2.3的安装为例
常用的方法
pssh使用帮助
pssh查看所有服务器的uptime;-h list 指定了执行命令的机器列表;-A表示提示输入密码(如果机器都是ssh key打通的则无需加-A)
使用pscp向一堆机器分发文件
从一堆机器中拷贝文件到中心机器
批量上传
批量上传本地文件 linux-3.14.3.tar.xz 到服务器上的 /tmp 目录:
批量下载
批量下载服务器上的某文件到本地,不用担心重名问题,因为 pssh 已经建立了 grid01, grid02, …, grid05 目录来存放下载的文件:
批量同步
有时候我们需要保持开发机上(某目录里)的数据和服务器上的数据一致:
常见问题
如果你遇到这样的错误:
IOError: [Errno 4] Interrupted system call
建议升级python版本到python2.7
简介
pssh是一款开源的软件,使用python实现。用于批量ssh操作大批量机器。
pssh的项目地址 https://code.google.com/p/parallel-ssh/
安装
在pssh的项目主页找到相应的版本,下载到我们的服务器上,解压后执行python setup.py安装。下面以pssh-2.3的安装为例
wget 'https://parallel-ssh.googlecode.com/files/pssh-2.3.tar.gz'
tar -xzvf pssh-2.3.tar.gz
cd pssh-2.3
python setup.py install
tar -xzvf pssh-2.3.tar.gz
cd pssh-2.3
python setup.py install
常用的方法
pssh使用帮助
pssh --help
pssh查看所有服务器的uptime;-h list 指定了执行命令的机器列表;-A表示提示输入密码(如果机器都是ssh key打通的则无需加-A)
pssh -i -A -h list 'uptime'
使用pscp向一堆机器分发文件
pscp -h list localfile remote_dir
从一堆机器中拷贝文件到中心机器
pslurp -h list /etc/hosts local_dir
批量上传
批量上传本地文件 linux-3.14.3.tar.xz 到服务器上的 /tmp 目录:
$ pscp -l root -A -h grids linux-3.14.3.tar.xz /tmp/
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 05:56:16 [SUCCESS] grid01
[2] 05:56:16 [SUCCESS] grid03
[3] 05:57:04 [SUCCESS] grid05
[4] 05:57:04 [SUCCESS] grid04
[5] 05:57:05 [SUCCESS] grid02
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 05:56:16 [SUCCESS] grid01
[2] 05:56:16 [SUCCESS] grid03
[3] 05:57:04 [SUCCESS] grid05
[4] 05:57:04 [SUCCESS] grid04
[5] 05:57:05 [SUCCESS] grid02
批量下载
批量下载服务器上的某文件到本地,不用担心重名问题,因为 pssh 已经建立了 grid01, grid02, …, grid05 目录来存放下载的文件:
$ pslurp -l root -h grids -A /tmp/linux-3.14.3.tar.xz .
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 06:06:01 [SUCCESS] grid01
[2] 06:06:01 [SUCCESS] grid03
[3] 06:06:06 [SUCCESS] grid04
[4] 06:06:06 [SUCCESS] grid02
[5] 06:06:06 [SUCCESS] grid05
$ ls
grid01 grid02 grid03 grid04 grid05 grids linux-3.14.3.tar.xz parallel-ssh
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 06:06:01 [SUCCESS] grid01
[2] 06:06:01 [SUCCESS] grid03
[3] 06:06:06 [SUCCESS] grid04
[4] 06:06:06 [SUCCESS] grid02
[5] 06:06:06 [SUCCESS] grid05
$ ls
grid01 grid02 grid03 grid04 grid05 grids linux-3.14.3.tar.xz parallel-ssh
批量同步
有时候我们需要保持开发机上(某目录里)的数据和服务器上的数据一致:
$ prsync -l root -h grids -A -r develop/ /tmp/production/
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 06:12:52 [SUCCESS] grid05
[2] 06:12:52 [SUCCESS] grid01
[3] 06:12:52 [SUCCESS] grid04
[4] 06:12:52 [SUCCESS] grid02
[5] 06:12:52 [SUCCESS] grid03
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 06:12:52 [SUCCESS] grid05
[2] 06:12:52 [SUCCESS] grid01
[3] 06:12:52 [SUCCESS] grid04
[4] 06:12:52 [SUCCESS] grid02
[5] 06:12:52 [SUCCESS] grid03
常见问题
如果你遇到这样的错误:
IOError: [Errno 4] Interrupted system call
建议升级python版本到python2.7