pdsh 是并行远程命令执行工具,可同时向多台主机发送命令,适合集群运维。
安装
1
2
3
4
5
| # CentOS/RHEL
yum install -y pdsh
# Ubuntu/Debian
apt install -y pdsh
|
前置条件
配置免密 SSH:ssh-copy-id user@host
核心选项
| 选项 | 作用 |
|---|
-w hosts | 指定目标主机 |
-l user | 指定登录用户 |
-f num | 最大并行数 |
-q | 静默模式 |
主机指定方式
1
2
3
4
5
| pdsh -w node1,node2 date # 直接罗列
pdsh -w node[1-20] uptime # 范围
pdsh -w 192.168.1.[10-30] free -h # IP 范围
pdsh -w @hostlist.txt hostname # 从文件读取
pdsh -w node[1-20],^node5 cmd # 排除主机
|
常用示例
1
2
3
| pdsh -w node[1-30] uname -r # 查看内核版本
pdsh -w @hosts systemctl restart nginx # 批量重启服务
pdsh -w node[1-20] df -h / | dshbak -c # 磁盘使用率并格式化
|
批量拷贝(pdcp)
1
2
| pdcp -w node[1-20] file.txt /tmp/ # 拷贝文件
pdcp -w node[1-20] -r /data/app /opt/ # 拷贝目录
|
注意事项
- 免密 SSH 是前提
- 用
-f 控制并行数 - 高危命令先单台测试
Comments