PSSH是一个可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的。使用前提是必须在各个服务器上配置好密钥认证访问,与ansible类似,不过比ansible轻量。
pssh 包安装 5 个实用程序:
pssh 在多个主机上并行地运行命令。
pscp 把文件并行地复制到多个主机上。
prsync 通过 rsync 协议把文件高效地并行复制到多个主机上。
pslurp 把文件并行地从多个远程主机复制到中心主机上。
pnuke 并行地在多个远程主机上杀死进程。
- 安装:
# yum install -y epel-release
# yum install -y pssh
- 建立SSH认证:
# vim ssh_auth.sh
#!/bin/bash
#批量实现SSH免密登录
#没有则安装expect
if ! rpm -q expect > /dev/null
then
echo "###expect 未安装,现在安装###"
yum install -y expect &>/dev/null
if [ $? -ne 0 ]
then
echo "###expect 安装失败###"
exit 1
fi
fi
#本机没有SSH密钥则生成
if [ ! -f ~/.ssh/id_rsa ]
then
echo "###请按3次enter键###"
ssh-keygen -t rsa
fi
ssh_expect () {
expect -c "set timeout -1;
spawn ssh-copy-id -f $1
expect {
"yes/no" { send -- yes\r;exp_continue;}
"password:" { send -- $2\r;exp_continue;}
eof
}";
}
[ -f hosts.txt ] && rm -rf hosts.txt
#定义 hosts.txt
cat > hosts.txt << EOF
192.168.30.128
192.168.30.129
192.168.30.130
EOF
passwd=123456789
for ip in `cat hosts.txt |awk '{print $1}'`
do
ssh_expect $ip $passwd
done
# sh ssh_auth.sh
这里以192.168.30.128/192.168.30.129/192.168.30.130三台机器为例。
pssh
pssh 在多个主机上并行地运行命令。
- pssh相关参数:
-h 主机文件列表,内容格式[user@]host[:port]。如test@172.16.10.10:229
-H 主机字符串,内容格式 user@ip:port
-l 远程机器的用户名
-p 并发的线程数
-P 执行时输出执行信息 (大写)
-o 输出内容重定向到一个文件
-e 执行错误重定向到一个文件
-t 设置命令执行超时时间,0无限制
-A 提示输入密码并且把密码传递给 ssh(如果私钥也有密码也用这个参数)
-O 设置 ssh 一些选项
-x 设置 ssh 额外的一些参数,可以多个,不同参数间空格分开
-X 同-x,但是只能设置一个参数 (大写)
-i 显示标准输出和标准错误在每台主机执行完毕后
常用选项:-h、-H、-i
- pssh示例:
# pssh -h hosts.txt uname -i
[1] 18:21:28 [SUCCESS] 192.168.30.130
[2] 18:21:28 [SUCCESS] 192.168.30.129
[3] 18:21:30 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i uname -i
[1] 18:21:36 [SUCCESS] 192.168.30.129
x86_64
[2] 18:21:36 [SUCCESS] 192.168.30.130
x86_64
[3] 18:21:38 [SUCCESS] 192.168.30.128
x86_64
# pssh -H 192.168.30.130 -i netstat -lntp
[1] 18:23:44 [SUCCESS] 192.168.30.130
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5103/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 5271/master
tcp6 0 0 :::22 :::* LISTEN 5103/sshd
tcp6 0 0 ::1:25 :::* LISTEN 5271/master
pscp
pscp 把文件并行地复制到多个主机上。
- 常用选项:
-r 递归复制目录
更多参数与pssh一致。
- pscp示例:
# echo 123123 > /home/1.txt
# pscp.pssh -h hosts.txt -l root /home/1.txt /home/
[1] 18:32:07 [SUCCESS] 192.168.30.129
[2] 18:32:07 [SUCCESS] 192.168.30.130
[3] 18:32:08 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i cat /home/1.txt
[1] 18:36:47 [SUCCESS] 192.168.30.130
123123
[2] 18:36:47 [SUCCESS] 192.168.30.129
123123
[3] 18:36:47 [SUCCESS] 192.168.30.128
123123
prsync
prsync 通过 rsync 协议把文件高效地并行复制到多个主机上。
- 常用参数:
-r 递归复制目录
-a 归档模式,同步过程保持源文件属性
-z 压缩传输
更多参数与pssh一致。
- prsync示例:
不使用 -a
# echo 123456 > /home/111.txt
# prsync -h hosts.txt /home/111.txt /home/
[1] 09:54:06 [SUCCESS] 192.168.30.130
[2] 09:54:06 [SUCCESS] 192.168.30.129
[3] 09:54:12 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i ls -l /home/111.txt
[1] 09:54:19 [SUCCESS] 192.168.30.130
-rw-r--r-- 1 root root 7 Apr 11 09:54 /home/111.txt
[2] 09:54:19 [SUCCESS] 192.168.30.129
-rw-r--r-- 1 root root 7 Apr 11 09:54 /home/111.txt
[3] 09:54:24 [SUCCESS] 192.168.30.128
-rw-r--r-- 1 root root 7 Apr 11 09:49 /home/111.txt
使用 -a
# prsync -h hosts.txt -a /home/111.txt /home/
[1] 09:50:24 [SUCCESS] 192.168.30.129
[2] 09:50:24 [SUCCESS] 192.168.30.130
[3] 09:50:29 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i cat /home/111.txt
[1] 09:50:40 [SUCCESS] 192.168.30.130
123456
[2] 09:50:40 [SUCCESS] 192.168.30.129
123456
[3] 09:50:46 [SUCCESS] 192.168.30.128
123456
# pssh -h hosts.txt -i ls -l /home/111.txt
[1] 09:52:11 [SUCCESS] 192.168.30.130
-rw-r--r-- 1 root root 7 Apr 11 09:49 /home/111.txt
[2] 09:52:11 [SUCCESS] 192.168.30.129
-rw-r--r-- 1 root root 7 Apr 11 09:49 /home/111.txt
[3] 09:52:16 [SUCCESS] 192.168.30.128
-rw-r--r-- 1 root root 7 Apr 11 09:49 /home/111.txt
pslurp
pslurp 把文件并行地从多个远程主机复制到中心主机上。
- 常用选项:
-r 递归复制目录
-L 指定远程文件复制的输出目录
更多参数与pssh一致。
- pslurp示例:
# pslurp -h hosts.txt -l root -L ./outdir /home/111.txt 123.txt
[1] 10:13:59 [SUCCESS] 192.168.30.130
[2] 10:13:59 [SUCCESS] 192.168.30.129
[3] 10:14:04 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i ls outdir/
[1] 10:16:00 [FAILURE] 192.168.30.129 Exited with error code 2
Stderr: ls: cannot access outdir/: No such file or directory
[2] 10:16:00 [FAILURE] 192.168.30.130 Exited with error code 2
Stderr: ls: cannot access outdir/: No such file or directory
[3] 10:16:05 [SUCCESS] 192.168.30.128
192.168.30.128
192.168.30.129
192.168.30.130
# pssh -H 192.168.30.128 -i ls outdir/192.168.30.128/
[1] 10:17:03 [SUCCESS] 192.168.30.128
123.txt
# pssh -H 192.168.30.128 -i cat outdir/192.168.30.128/123.txt
[1] 10:17:21 [SUCCESS] 192.168.30.128
123456
pnuke
pnuke 并行地在多个远程主机上杀死进程。
更多参数与pssh一致。
- pnuke示例:
# pssh -h hosts.txt "yum install -y nginx && systemctl start nginx"
[1] 10:29:06 [SUCCESS] 192.168.30.129
[2] 10:29:06 [SUCCESS] 192.168.30.130
[3] 10:29:08 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i netstat -lntp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6713/nginx: master
tcp6 0 0 :::80 :::* LISTEN 6713/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6689/nginx: master
tcp6 0 0 :::80 :::* LISTEN 6689/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15826/nginx: master
tcp6 0 0 :::80 :::* LISTEN 15826/nginx: master
# pnuke -h hosts.txt -l root nginx
[1] 10:34:18 [SUCCESS] 192.168.30.130
[2] 10:34:18 [SUCCESS] 192.168.30.129
[3] 10:34:23 [SUCCESS] 192.168.30.128
# pssh -h hosts.txt -i netstat -lntp |grep nginx
PSSH是一个值得使用的轻量级自动化工具,相比Ansible,PSSH在命令行下更加便捷。