LoginSignup
0
0

テキストエディタでコマンドを作って貼るのをやめる。

Last updated at Posted at 2024-01-05

はじめに

複数のIPアドレスに対して同じようなコマンドを大量に実行する場合に、テキストエディタで編集して貼り付けることで対応出来ますが、ループで実行したほうが簡単なのでその方法を記載しておきます。

説明

この例では複数のIPアドレスに対して再起動コマンドを実行する例を記載します。

以下のコマンドを実行すると入力待ちになります。

[root@ol72 ~]# while read ip
> do
> ssh root@${ip} 'shutdown -r now'
> sleep 1
> done

IPアドレスのリストを貼り付けます。

192.168.100.101
192.168.100.102
192.168.100.103

1行読み込む(貼り付ける)ごとにコマンドが実行されます。
この例では実際には以下のコマンドが実行されます。

ssh root@192.168.100.101 'shutdown -r now'
ssh root@192.168.100.102 'shutdown -r now'
ssh root@192.168.100.103 'shutdown -r now'

SSH接続をしているので公開鍵認証の設定をしておくと良いかと思います。

最終行まで読み込むと再度入力待ちとなりますので、Ctrl-Dを入力するとプロンプトに戻ります。

Ctrl-D
[root@ol72 ~]#

テキストエディタでコマンドを作成していると編集する行数も多く誤りが発生する事があるので上記の方法のほうが良いかと思います。

0
0
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
0
0