一、简单介绍
1、pssh全称是parallel-ssh,基于Python编写的并发在多台服务器上批量执行命令的工具。包括pssh,pscp,prsync,pnuke和pslurp。该项目包括psshlib,可以在自定义应用程序中使用。它相当于ansible的简化版,执行起来速度比ansible快,支持文件并行复制,远程命令执行,杀掉远程主机上的进程,杀手锏是文件并行复制。
2、相关参数
  -h –hosts 主机文件列表,内容格式”[user@]host[:port]”
  -H –host 单台主机,内容格式”[user@]host[:port]”
  -l –user 登录使用的用户名
  -p –par 并发的线程数【可选】
  -o –outdir 输出的文件目录【可选】
  -e –errdir 错误输入文件【可选】
  -t –timeout TIMEOUT 超时时间设置,0无限制【可选】
  -O  –option SSH的选项
  -v –verbose 详细模式
  -A –askpass 手动输入密码模式
  -x –extra-args 额外的命令行参数使用空白符号,引号,反斜线处理
  -X –extra-arg 额外的命令行参数,单个参数模式,同-x
  -i –inline 每个服务器内部处理信息输出
  –inline-stdout 每个服务器的内服输出
  -P, –print 打印出服务器返回信息
3、主要用法:
  1.pssh命令    在远程主机上执行本地命令或者脚本
  2.pscp命令    将本地文件拷贝至多个远端主机
  3.pslurp命令  从多台远程机器拷贝文件到本地
  4.pnuke命令  并行在远端主机杀进程
  5.prsync命令  使用rsync协议从本地计算机同步到远程主机
二、环境
1、[root@localhost ~]# vim ip.txt (创建要登录的ip文本)
129.168.40.132(pssh机) 192.168.40.220
192.168.40.155 192.168.40.211
2、免密登录脚本
[root@localhost ~]# vim ssh_key.sh
#!/bin/bash
rpm -q expect &> /dev/null || yum install expect -y
ssh-keygen -p "" -f "/root/.ssh/id_rsa"
password=xxxxx  (四台机子密码一样)
while read ipaddr;do
expect <<EOF
set timeout 10 
spawn ssh-copy-id $ipaddr 
expect {
"yes/no"  { send "yes\";exp_continue }
"password" { send "$password\n" }
} 
expect eof
EOF
done < ip.txt
3、互相能通信上网
三、示例
1、第一次没有登录时(没有做免密操作,需要# ssh 192.168.40.155登录,输入一次yes)
[root@localhost ~]# pssh -H "192.168.40.220 192.168.40.155" -A  -i  hostname
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 09:04:44 [SUCCESS] 192.168.40.155
php2
[2] 09:04:44 [SUCCESS] 192.168.40.220
zjz
2、免密登录后,批量创建用户
[root@localhost ~]# pssh  -h   ip.txt   -i  ‘useradd pssher‘
[1] 09:23:06 [SUCCESS] 192.168.40.155
[2] 09:23:07 [SUCCESS] 192.168.40.211
[3] 09:23:07 [SUCCESS] 192.168.40.220
[4] 09:23:07 [SUCCESS] 192.168.40.132
[root@localhost ~]# pssh   -h  ip.txt   -i  ‘getent  passwd  pssher‘  (验证创建用户是否成功)
3、指定输出信息存放文件夹(此文件夹不需要提前创建)
[root@localhost ~]# pssh  -h  ip.txt  -o  /data/    ‘cat /etc/fstab‘
[1] 09:40:56 [SUCCESS] 192.168.40.132
[2] 09:40:56 [SUCCESS] 192.168.40.220
[3] 09:40:56 [SUCCESS] 192.168.40.155
[4] 09:40:56 [SUCCESS] 192.168.40.211
[root@localhost ~]# cd /data
[root@localhost data]# ls
192.168.40.132  192.168.40.155  192.168.40.211  192.168.40.220
4、关闭selinux
[root@localhost ~]# pssh -h ip.txt  ‘sed -i "s/^SELINUX=.*/SELINUX=disabled/"  /etc/selinux/config‘
[1] 10:00:18 [SUCCESS] 192.168.40.220
[2] 10:00:18 [SUCCESS] 192.168.40.155
[3] 10:00:18 [SUCCESS] 192.168.40.211
[4] 10:00:18 [SUCCESS] 192.168.40.132
[root@localhost ~]# cat /etc/selinux/config  (查看结果,不用getenforce,刷新太慢)
5、pscp.pssh远程推送
[root@localhost ~]# pssh -h ip.txt  ‘mkdir /data‘
[1] 10:13:39 [SUCCESS] 192.168.40.220
[2] 10:13:39 [SUCCESS] 192.168.40.155
[3] 10:13:39 [SUCCESS] 192.168.40.211
[4] 10:13:39 [SUCCESS] 192.168.40.132
[root@localhost ~]# pscp.pssh -h ip.txt /root/zj.sh  /data/
[1] 10:13:52 [SUCCESS] 192.168.40.220
[2] 10:13:52 [SUCCESS] 192.168.40.155
[3] 10:13:52 [SUCCESS] 192.168.40.211
[4] 10:13:52 [SUCCESS] 192.168.40.132
[root@localhost ~]# pssh -h ip.txt  ‘bash /data/zj.sh‘
[1] 10:14:25 [SUCCESS] 192.168.40.220
[2] 10:14:25 [SUCCESS] 192.168.40.155
[3] 10:14:25 [SUCCESS] 192.168.40.211
[4] 10:14:25 [SUCCESS] 192.168.40.132
[root@localhost ~]# pssh -h ip.txt -i ‘bash /data/zj.sh‘
[1] 10:14:31 [SUCCESS] 192.168.40.220
zjz
[2] 10:14:31 [SUCCESS] 192.168.40.211
localhost.localdomain
[3] 10:14:31 [SUCCESS] 192.168.40.155
php2
[4] 10:14:31 [SUCCESS] 192.168.40.132
localhost.localdomain
6、pslurp远程下载(拉取)
[root@localhost ~]# pslurp -h ip.txt   -L /a    /var/log/messages   messages (-L指定文件夹,最后跟上自定义名,如messages)
[1] 10:19:25 [SUCCESS] 192.168.40.155 
[2] 10:19:25 [SUCCESS] 192.168.40.211
[3] 10:19:25 [SUCCESS] 192.168.40.220
[4] 10:19:25 [SUCCESS] 192.168.40.132
[root@localhost ~]# ls /a
192.168.40.132  192.168.40.155  192.168.40.211  192.168.40.220
[root@localhost ~]# tree /a/
/a/
├── 192.168.40.132
│   └── messages
├── 192.168.40.155
│   └── messages
├── 192.168.40.211
│   └── messages
└── 192.168.40.220
    └── messages
[root@localhost ~]# pslurp -h ip.txt  -L /aa -r  /var/log  m  (-r 递归下载)
7、pnuke 并行在远程主机杀进程
[root@localhost ~]# pnuke   -h   ip.txt   httpd
[1] 11:18:04 [SUCCESS] 192.168.40.211
[2] 11:18:04 [SUCCESS] 192.168.40.155
[3] 11:18:04 [SUCCESS] 192.168.40.220
[4] 11:18:05 [SUCCESS] 192.168.40.132
8、prsync -r递归将/home传到各主机 /tmp/pssh/目录下
[root@localhost ~]# prsync  -h ip.txt -r /home /tmp/pssh/
[1] 11:24:26 [SUCCESS] 192.168.40.132
[2] 11:24:26 [SUCCESS] 192.168.40.220
[3] 11:24:26 [SUCCESS] 192.168.40.155
[4] 11:24:26 [SUCCESS] 192.168.40.211
四、rsync工具
-v:显示rsync过程中详细信息。
-P:显示文件传输的进度信息。
-n :模拟复制过程。
-a :归档模式,表示递归传输并保持文件属性。等同于"-rtopgDl"。
-t :保持mtime属性。建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新 :检查出mtime不同从而导致增量传输无效。
-o :保持owner属性(属主)。
-g --group:保持group属性(属组)。
-p --perms:保持perms属性(权限,不包括特殊权限)。
-D :是"--device --specials"选项的组合,即也拷贝设备文件和特殊文件。
-l --links:如果文件是软链接文件,则拷贝软链接本身而非软链接所指向的对象。
-z :传输时进行压缩提高效率。
-R :使用相对路径。
-u :仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为。
-d :以不递归的方式拷贝目录本身。
-e :指定所要使用的远程shell程序,默认为ssh。
-W :rsync将不再使用增量传输,而是全量传输。
rsync做远程传输时比scp更高效,rsync基于增量传输,更快,效率更高。
[root@zjz ~]# rsync -av /root/zjz/*  192.168.40.211:/root/cui/
root@192.168.40.211‘s password: 
sending incremental file list
f1   (/root/zjz/有f1,f2,f3,三个文件,f1被发送是因为只有f1发生改变)
sent 133907 bytes  received 189185 bytes  15027.53 bytes/sec
total size is 2826960896  speedup is 8749.71
原文:https://www.cnblogs.com/zjz20/p/11619566.html