CentOS Stream 10
Sponsored Link

OpenSSH : Parallel SSH を利用する2024/12/19

 

PSSH (Parallel Secure Shell) を使用して、SSH サーバーが稼働する複数ホストに対して並列で SSH 接続します。

[1] PSSH をインストールします。
# EPEL からインストール

[root@dlp ~]#
dnf --enablerepo=epel -y install pssh
[2] PSSH の使い方です。
各対象サーバーに対して、パスフレーズ無し鍵ペアで認証可能であることを前提とします。
パスフレーズ付き鍵ペア認証の場合は 事前に SSH-Agent を起動してパスフレーズをセットしておけば、以下と同様の操作で利用可能です。
# 指定のホストに対して SSH 接続し hostname コマンドを実行

[cent@dlp ~]$
pssh -H "10.0.0.51 10.0.0.52" -i "hostname"

[1] 13:17:26 [SUCCESS] 10.0.0.52
node02.srv.world
[2] 13:17:26 [SUCCESS] 10.0.0.51
node01.srv.world

# 接続先ホストはファイルからも読み込み可能

[cent@dlp ~]$
vi pssh_hosts.txt
# 接続先ホストを 1 行ごとに記述

cent@10.0.0.51
cent@10.0.0.52
[cent@dlp ~]$
pssh -h pssh_hosts.txt -i "uptime"

[1] 13:18:47 [SUCCESS] cent@10.0.0.51
 13:18:47 up  2:33,  4 users,  load average: 0.00, 0.00, 0.00
[2] 13:18:47 [SUCCESS] cent@10.0.0.52
 13:18:47 up 7 min,  4 users,  load average: 0.00, 0.00, 0.00
[3] パスワード認証で接続したい場合はオプション指定で可能ですが、接続先ホストの接続ユーザーのパスワードは全ホストで同じである必要があります。
# 認証の優先順位をパスワード優先にして -A でパスワード認証

[cent@dlp ~]$
pssh -h pssh_hosts.txt -A -O PreferredAuthentications=password -i "uname -r"

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:   # パスワード入力
[1] 13:19:28 [SUCCESS] cent@10.0.0.51
6.12.0-32.el10.x86_64
[2] 13:19:28 [SUCCESS] cent@10.0.0.52
6.12.0-32.el10.x86_64
[4]
なお、PSSH パッケージには pssh コマンド以外にも、pscp.pssh, prsync, pslurp, pnuke も含まれており、 [pssh option ~] の部分はいずれも pssh と同様の書式で利用可能です。
関連コンテンツ