pdsh是一個多線程遠程shell客戶機,它在多個遠程主機上並行執行命令
pdsh可以使用幾種不同的遠程shell服務,包括標准的 rsh、Kerberos IV 和 ssh
- 在使用
pdsh之前,必須保證本地主機和要管理遠程主機之間的單向信任
pdsh還附帶了pdcp命令,該命令可以將本地文件批量復制到遠程的多台主機上,這在大規模的文件分發環境下非常有用
- github:
https://github.com/grondo/pdsh
安裝
CentOS系列可以使用yum安裝,pdsh需要epel源
Linux:~
Linux:~
Linux:~
Linux:/usr/local/src/pdsh-pdsh-2.31
--prefix=/usr/local/pdsh \
--with-ssh \
--with-machines=/usr/local/pdsh/machines \
--with-dshgroups=/usr/local/pdsh/group \
--with-rcmd-rank-list=ssh \
--with-exec && \
make && \
make install
--with-ssh ssh模塊(支持ssh)
--with-rcmd-rank-list=ssh 指定默認模式為ssh
--with-dshgroups= 指定默認主機組路徑
--with-machines= 指定默認主機列表
- 在該文件中寫入主機地址(或主機名,需要在hosts中寫好主機解析),每行一個
- 存在machines文件,使用
pdsh執行時若不指定主機,則默認對machines文件中所有主機執行該命令
--with-exec exec模塊
- 其他模塊參數可以在
pdsh-pdsh-2.31目錄下使用 ./configure --help 命令查看
Linux:~
total 516
-rwxr-xr-x 1 root root 8638 Jan 29 22:15 dshbak
-rwxr-xr-x 1 root root 171664 Jan 29 22:15 pdcp
-rwxr-xr-x 1 root root 171664 Jan 29 22:15 pdsh
-rwxr-xr-x 1 root root 171664 Jan 29 22:15 rpdcp
Linux:~
Linux:~
"將pdsh的所有命令追加到環境變量中"
Linux:~
pdsh-2.31
rcmd modules: ssh,rsh,exec (default: ssh)
misc modules: machines,dshgroup
使用
- 語法:
pdsh <參數> <需要並行執行的命令>
- 如果只輸入前面兩部分,回車后可進入pdsh交互式命令行(若是編譯安裝需要啟用
--with-readline),再輸入並行執行的命令部分
- 常用參數:
-w 指定主機 -x 排除指定的主機
- 目標主機可以使用Ip地址或主機名(確保該主機名已經在
/etc/hosts中存在解析)
- 多個主機之間可以使用逗號分隔,可重復使用該參數指定多個主機;可以使用簡單的正則
-g 指定主機組 -G 排除指定主機組
-l 目標主機的用戶名
- 如果不指定用戶名,默認以當前用戶名作為在目標主機上執行命令的用戶名
-N 用來關閉目標主機所返回值前的主機名顯示
示例
-w 指定主機
Linux:~
192.168.72.12: Sun Jan 31 12:35:36 CST 2021
192.168.72.14: Sun Jan 31 12:35:36 CST 2021
192.168.72.13: Sun Jan 31 12:35:36 CST 2021
"pdsh -w ssh:192.168.72.[12-14] date 也可以"
-l 指定用戶
Linux:~
192.168.72.12: Sun Jan 31 12:36:32 CST 2021
192.168.72.13: Sun Jan 31 12:36:32 CST 2021
192.168.72.14: Sun Jan 31 12:36:32 CST 2021
-g指定用戶組
Linux:~
Linux:~
192.168.72.12
192.168.72.13
192.168.72.14
EOF
Linux:~
192.168.72.12: 4.4.73-5-default
192.168.72.14: 4.4.73-5-default
192.168.72.13: 4.4.73-5-default
主機列表
Linux:~
192.168.72.12
192.168.72.13
192.168.72.14
EOF
Linux:~
192.168.72.12: 12:37pm up 0:08, 2 users, load average: 0.08, 0.13, 0.09
192.168.72.13: 12:37pm up 0:07, 1 user, load average: 0.12, 0.05, 0.01
192.168.72.14: 12:37pm up 0:07, 1 user, load average: 0.00, 0.01, 0.00
交互式界面
"有exec模塊即可,或者readline模塊"
Linux:~
pdsh> date
192.168.72.14: Sun Jan 31 12:38:05 CST 2021
192.168.72.13: Sun Jan 31 12:38:05 CST 2021
192.168.72.12: Sun Jan 31 12:38:05 CST 2021
pdsh> whoami
192.168.72.12: root
192.168.72.14: root
192.168.72.13: root
pdsh> exit "退出交互式界面"