LoginSignup
9
10

More than 5 years have passed since last update.

Parallel Distributed Shell(pdsh)を使って複数ホストに並列コマンド実行する

Last updated at Posted at 2014-03-30

インストール

EPELリポジトリからインストール可能。

$ sudo yum --enablerepo=epel install pdsh
(略)
$ pdsh -h
Usage: pdsh [-options] command ...
-S                return largest of remote command return values
-h                output usage menu and quit
-V                output version information and quit
-q                list the option settings and quit
-b                disable ^C status feature (batch mode)
-d                enable extra debug information from ^C status
-l user           execute remote commands as user
-t seconds        set connect timeout (default is 10 sec)
-u seconds        set command timeout (no default)
-f n              use fanout of n nodes
-w host,host,...  set target node list on command line
-x host,host,...  set node exclusion list on command line
-R name           set rcmd module to name
-M name,...       select one or more misc modules to initialize first
-N                disable hostname: labels on output lines
-L                list info on all loaded modules and exit
available rcmd modules: ssh,exec (default: ssh)

モジュールの追加

モジュールの説明は割愛。

デフォルトでは、rcmdの二つのモジュールのみアクティブ。

$ pdsh -L
2 modules loaded:

Module: rcmd/exec
Author: Mark Grondona <mgrondona@llnl.gov>
Descr:  arbitrary command rcmd connect method
Active: yes

Module: rcmd/ssh
Author: Jim Garlick <garlick@llnl.gov>
Descr:  ssh based rcmd connect method
Active: yes

miscモジュールは入っていないので、インストールする。

$ sudo yum list --enablerepo=epel pdsh-mod-*
pdsh-mod-dshgroup.x86_64                2.26-4.el6                     epel     
pdsh-mod-genders.x86_64                 2.26-4.el6                     epel     
pdsh-mod-netgroup.x86_64                2.26-4.el6                     epel     
pdsh-mod-nodeupdown.x86_64              2.26-4.el6                     epel     
pdsh-mod-torque.x86_64                  2.26-4.el6                     epel 
$ sudo yum install --enablerepo=epel pdsh-mod-dshgroup
(略)
$ pdsh -L
3 modules loaded:

Module: misc/dshgroup
Author: Mark Grondona <mgrondona@llnl.gov>
Descr:  Read list of targets from dsh-style "group" files
Active: yes
Options:
-g groupname      target hosts in dsh group "groupname"
-X groupname      exclude hosts in dsh group "groupname"
(略)

使い方

基本

$ pdsh -w remote01 uptime
remote01:  14:03:14 up 15 min,  0 users,  load average: 0.00, 0.03, 0.05

複数台

カンマ区切り。

$ pdsh -w remote01,remote02 uptime
remote01:  14:06:27 up 18 min,  0 users,  load average: 0.00, 0.01, 0.05
remote02:  14:06:27 up 18 min,  0 users,  load average: 0.02, 0.04, 0.05

正規表現も可能。

$ pdsh -w remote0[1,2] uptime
remote01:  14:07:20 up 19 min,  0 users,  load average: 0.00, 0.01, 0.05
remote02:  14:07:20 up 19 min,  0 users,  load average: 0.01, 0.03, 0.05

グループ指定(misc/dshgroup)も可能。

$ cat ~/.dsh/group/remotes
remote01
remote02
$ pdsh -g remotes uptime
remote01:  14:08:50 up 19 min,  0 users,  load average: 0.00, 0.01, 0.05
remote02:  14:08:50 up 19 min,  0 users,  load average: 0.01, 0.03, 0.05

出力結果の整形

一緒にインストールされるdshbakコマンドを使って結果を見やすく整形できる。

整形なし

$ pdsh -w remote0[1,2] 'cat /etc/system-release'
remote01: Amazon Linux AMI release 2014.03
remote02: Amazon Linux AMI release 2014.03

整形あり

$ pdsh -w remote0[1,2] 'cat /etc/system-release' | dshbak 
----------------
remote01
----------------
Amazon Linux AMI release 2014.03
----------------
remote02
----------------
Amazon Linux AMI release 2014.03

整形あり(同じ結果はまとめる)

$ pdsh -w remote0[1,2] 'cat /etc/system-release' | dshbak -c
----------------
remote[01-02]
----------------
Amazon Linux AMI release 2014.03
9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10