LoginSignup
1
0

More than 5 years have passed since last update.

serverを巡回し、各サーバでdf; wを実行しサーバの状況を一括出力するスクリプト

Last updated at Posted at 2019-04-23

概要

リストに記述したサーバをssh接続で巡回し、各サーバで単純なコマンドを実行し、その出力を一括してlessで見る。

追記:実行中に出力を見られるように、teeで改変

環境

  • スクリプト実行:MacOS (Mojave) GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
  • サーバ群:Ubuntu, Debian, CentOS, MacOS, Raspbian

準備

  • 各hostは鍵認証でsshログインできるように、あらかじめ設定しておきます。
  • ~/.ptrlにホスト名を列挙しておきます。
~/.ptrl
host1
host2
.
.
hostn

スクリプト

  • cmmnd1には、実行したいコマンドを記述しておきます。
~/bin/ptrl
#!/bin/bash
hosts=`cat ~/.ptrl`
cmmnd1="df -H; w; exit"
logf="/tmp/ptrl"

date >$logf
for host in $hosts;
do
        echo "----------------------------"
        echo $host
        echo "----------------------------"
        echo "$cmmnd1" |ssh $host
#done >>$logf 
#コマンド実行中に出力をみられるように改変
done |tee -a $logf
less $logf
  • /tmp/ptrlには、最新の巡回結果を残しておきます。

出力例

2019年 4月23日 火曜日 23時52分40秒 JST
----------------------------
host1
----------------------------
Linux host1 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Filesystem      Size  Used Avail Use% Mounted on
udev            488M     0  488M   0% /dev
tmpfs           100M   13M   88M  13% /run
/dev/vda2        50G  7.9G   39G  17% /
tmpfs           499M     0  499M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           499M     0  499M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000
 23:52:40 up 1 day, 13:40,  2 users,  load average: 0.00, 0.05, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
----------------------------
host2
----------------------------
Linux host2 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
ファイルシス   サイズ  使用  残り 使用% マウント位置
udev             488M     0  488M    0% /dev
tmpfs            100M   12M   88M   13% /run
/dev/vda2         50G  6.7G   41G   15% /
tmpfs            499M     0  499M    0% /dev/shm
tmpfs            5.0M     0  5.0M    0% /run/lock
tmpfs            499M     0  499M    0% /sys/fs/cgroup
tmpfs            100M     0  100M    0% /run/user/1000
 23:52:41 up 1 day,  1:34,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
<以下略>
1
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
1
0