LoginSignup
4
4

More than 5 years have passed since last update.

SSH による 複数 ホスト への 連続的 な コマンド 実行

Last updated at Posted at 2016-06-06

sshでは、ホストにログインせずにコマンドを指定して実行結果を得ることができます。

 ssh -l hoge 192.168.0.5 "date;hostname"

各対象ホストにコマンド「ntpdate -q」を連続的に実行させる場合は


$ for i in 192.168.0.5 192.168.8.2 10.0.1.101; do ssh hoge@${i} "hostname; /usr/sbin/ntpdate -q nict.go.jp"; done

テキストファイルからIPアドレスを抽出する場合
テキストファイル「host.txt」


192.168.0.5
192.168.8.2
10.0.1.101

$ ssh2list() { cmd=$*; for i in `cat host.txt`;do ssh hoge@${i} ${cmd}; done; }
$ ssh2list "hostname; df -h; free; w; date"
4
4
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
4
4