一、介绍
1.pssh介绍
- pssh是python写的可以并发在多台机器上批量执行命令的工具,它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持文件并行复制,远程命令执行,杀掉远程主机上的进程等等。
2验环境:
- 系统:Centos6.5
- 虚拟机:三个
- 主机名,ip:
- rsync-A 10.0.0.129(管理)
- rsync-B 10.0.0.130
rsync-C 10.0.0.131
3在rsync-A上生秘钥
[root@rsync-A ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ca:3d:85:7f:21:88:56:31:51:6c:1d:65:87:86:d4:e8 root@rsync-A
The key's randomart image is:
+--[ RSA 2048]----+
| ++.oo*o..|
| oo +.+. |
| .. . . |
| o o E |
| o S o . |
| o o o . . |
| o o . . |
| . . |
| |
+-----------------+
3.将秘钥发送到另外两台虚拟机
[root@rsync-A ~]# for ip in `seq 130 131`;do ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.$ip;done
二、安装pssh工具
1.下载pssh包
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/parallel-ssh/pssh-2.3.1.tar.gz
2.解压安装
[root@rsync-A ~]# tar xf pssh-2.3.1.tar.gz
[root@rsync-A ~]# cd pssh-2.3.1
[root@rsync-A pssh-2.3.1]# ls
AUTHORS bin ChangeLog COPYING INSTALL man PKG-INFO psshlib setup.py test
[root@rsync-A pssh-2.3.1]# python setup.py install
3.编译安装完成后,可以使用的命令
pssh:在远程多台主机上并行运行命令
pscp :把文件并行复制到多台远程主机上
prsync:使用rsync协议本地文件同步到远程多台主机上。
pnuke:在远程多台主机上并行killall某一进程
pslurp:把文件从远程多台主机上复制到本地主机上
4.常用参数介绍
[root@rsync-A ~]# pssh --help
-h HOST_FILE 后边跟远程主机列表(ip)
-H HOST_STRING 后边跟远程主机名或者ip地址
-l USER 指定远程主机的用户名
-p PAR 指定pssh最大的并行线程数。
-o 将输出的内容重定向到一个指定的文件中
-O 指定ssh参数的具体配置
-e 将执行错误重定向到一个指定的文件中
-t 设定命令执行超时时间
-x 传递ssh命令的一些参数
-i 在远程主机上执行命令完成后显示标准输出和标准错误
-P 在执行远程命令时,输出执行结果
5.测试
5.1创建ip文本
[root@rsync-A ~]# cat >>ip.txt<<EOF
> 10.0.0.130
> 10.0.0.131
> EOF
>
5.2查看磁盘使用情况
[root@rsync-A ~]# pssh -i -h ip.txt "df -h"()
[1] 11:34:22 [SUCCESS] 10.0.0.130
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_liang-lv_root 8.4G 935M 7.1G 12% /
tmpfs 503M 0 503M 0% /dev/shm
/dev/sda1 485M 31M 429M 7% /boot
10.0.0.129:/liang 8.4G 801M 7.2G 10% /data
[2] 11:34:22 [SUCCESS] 10.0.0.131
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_liang-lv_root 8.4G 700M 7.3G 9% /
tmpfs 503M 0 503M 0% /dev/shm
/dev/sda1 485M 31M 429M 7% /boot
5.3复制测试(要所有主机都要scp命令)
[root@rsync-A ~]# pssh -i -h ip.txt "yum install openssh-clients -y"
[root@rsync-A ~]# pscp -h ip.txt /etc/hosts /opt
[1] 11:50:31 [SUCCESS] 10.0.0.130
[2] 11:50:31 [SUCCESS] 10.0.0.131
[root@rsync-A ~]# pssh -i -h ip.txt "ls /opt"
[1] 11:50:48 [SUCCESS] 10.0.0.131
hosts
[2] 11:50:48 [SUCCESS] 10.0.0.130
a.txt
hosts
5.4杀服务
[root@rsync-A ~]# pssh -i -h ip.txt "/etc/init.d/crond status"
[1] 11:52:46 [SUCCESS] 10.0.0.131
crond (pid 1100) is running...
[2] 11:52:46 [SUCCESS] 10.0.0.130
crond (pid 1094) is running...
[root@rsync-A ~]# pnuke -h ip.txt crond
[1] 11:53:00 [SUCCESS] 10.0.0.130
[2] 11:53:00 [SUCCESS] 10.0.0.131
[root@rsync-A ~]# pssh -i -h ip.txt "/etc/init.d/crond status"
[1] 11:53:02 [FAILURE] 10.0.0.130 Exited with error code 1
crond dead but pid file exists
[2] 11:53:02 [FAILURE] 10.0.0.131 Exited with error code 1
crond dead but pid file exists