ssh(2)pssh(parallel ssh)
pssh
(Parallel SSH)是一款用于在多台服务器上并行执行命令的工具,特别适用于需要远程管理多台机器的场景。它比普通的 ssh
更高效,尤其是批量操作时。
1.安装
在 Linux 系统(如 Ubuntu、CentOS)上,使用以下命令安装:
Ubuntu/Debian:
1 | sudo apt update |
CentOS/RHEL:
1 | sudo yum install pssh |
编译安装
第三方维护pssh源码
env
- centos8.x
- python3.x
1.download
https://github.com/robinbowes/pssh/archive/refs/tags/v2.1.1.tar.gz
2.required
dnf install -y python3
3.setup
[root@centos8-51 pssh-2.1.1]# python3 setup.py install
...
Adding pssh 2.1.1 to easy-install.pth file
Installing pnuke script to /usr/local/bin
Installing prsync script to /usr/local/bin
Installing pscp script to /usr/local/bin # 本地文件复制到远程
Installing pslurp script to /usr/local/bin # 远程下载文件到本地
Installing pssh script to /usr/local/bin # 执行命令
Installing pssh-askpass script to /usr/local/bin
2.基本用法
1 | pssh -h <host_file> -l <user> -p <parallel_connections> -i "<command>" |
参数说明
-h <host_file>
:指定包含服务器 IP/主机名的文件(每行一个主机)。-l <user>
:指定远程服务器的用户名。-p <parallel_connections>
:设置并行连接数(默认 32)。-i
:显示每台服务器的输出(交互模式)。-t <timeout>
:设置超时时间(默认 60 秒)。-O <options>
:传递额外的 SSH 选项。-A
:在命令执行前提示输入密码(更安全)。
pssh help
[root@centos8-51 ~]# pssh
pssh pssh-askpass
[root@centos8-51 ~]# pssh -h
Usage: pssh [OPTIONS] command [...]
pssh: error: -h option requires 1 argument
[root@centos8-51 ~]# pssh --help
Usage: pssh [OPTIONS] command [...]
Options:
--help show this help message and exit
-h HOST_FILES, --hosts=HOST_FILES ### 基于文件形式
hosts file (each line "host[:port] [user]")
-H HOST_ENTRY, --host=HOST_ENTRY ### 基于字符串
additional host entry ("[user@]host[:port]")
-l USER, --user=USER username (OPTIONAL) #### 用户名
-p PAR, --par=PAR max number of parallel threads (OPTIONAL) ### 并行数
-o OUTDIR, --outdir=OUTDIR ### 输出保存目录
output directory for stdout files (OPTIONAL)
-e ERRDIR, --errdir=ERRDIR ### 错误保存目录
output directory for stderr files (OPTIONAL)
-t TIMEOUT, --timeout=TIMEOUT
timeout (secs) (-1 = no timeout) per host (OPTIONAL)
-O OPTIONS, --options=OPTIONS
SSH options (OPTIONAL)
-v, --verbose turn on warning and diagnostic messages (OPTIONAL) ### 输出详细日志
-A, --askpass Ask for a password (OPTIONAL) ### 手工输入密码模式
-x ARGS, --extra-args=ARGS ### 命令传入参数
Extra command-line arguments, with processing for
spaces, quotes, and backslashes
-X ARG, --extra-arg=ARG
Extra command-line argument
-P, --print print output as we get it
-i, --inline inline aggregated output for each server ### 每个服务端处理信息输出
-I, --send-input read from standard input and send as input to ssh
Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
3.示例
- 创建主机文件
1 | cat > hosts.txt <<EOF |
- 在多台服务器上并行执行命令
1 | pssh -h hosts.txt -l root -p 3 -i "uptime" |
输出示例:
[1] 21:10:01 [SUCCESS] 192.168.1.101
21:10:01 up 10 days, 3:45, 2 users, load average: 0.12, 0.08, 0.05
[2] 21:10:01 [SUCCESS] 192.168.1.102
21:10:01 up 5 days, 7:32, 1 user, load average: 0.04, 0.02, 0.01
[3] 21:10:01 [SUCCESS] 192.168.1.103
21:10:01 up 15 days, 6:21, 0 users, load average: 0.00, 0.00, 0.00
- 执行带 sudo 的命令
1 | pssh -h hosts.txt -l root -i "sudo systemctl restart nginx" |
- 从文件中读取命令并执行
1 | pssh -h hosts.txt -l root -I < command.txt |
- 传输文件
虽然pssh
本身不支持文件传输,但你可以搭配pscp.pssh
使用:
1 | pscp.pssh -h hosts.txt -l root local_file /remote/path/ |
4.注意事项
- 确保你的 SSH 免密登录已配置好,或使用
-A
选项以交互方式输入密码。 pssh
输出结果按服务器分别标注,方便查看哪些机器执行成功或失败。