![毎週木曜日のレポートを生成するスクリプトの作成 [閉じる]](https://linux33.com/image/11144/%E6%AF%8E%E9%80%B1%E6%9C%A8%E6%9B%9C%E6%97%A5%E3%81%AE%E3%83%AC%E3%83%9D%E3%83%BC%E3%83%88%E3%82%92%E7%94%9F%E6%88%90%E3%81%99%E3%82%8B%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E4%BD%9C%E6%88%90%20%5B%E9%96%89%E3%81%98%E3%82%8B%5D.png)
毎週木曜日ごとに、18 台のサーバーに対して次のレポートを送信する必要があります。
アカウント:swebwpspサーバー名:nykpsr17896ディスク容量使用率:70%(df -khコマンドを使用)日付:2018-09-20
同様に、私たちは18のサーバーを持っています。
これを自動化できますか?
答え1
あなたはそれを使用することができます予約されたこと週に一度、特定の曜日にジョブを実行するなど、定期的にジョブを実行します。サーバーのリストを照会するスクリプトを作成し、レポートを保存できます。
crontab -e
ジョブを実行するユーザーとして実行します。たとえば、
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
0 0 * * thu /path/to/your/script
サーバーのリストを読み取り、SSH経由でコマンドを実行するスクリプト例:
#!/bin/sh
report="report-$(date -I)" # report file
servers="servers.txt" # file containing a list of hosts
command="df -hk" # command to execute at remote host
exec 1>$report # redirect output to report file
exec 2>&1 # stderr to stdout
while IFS= read -r server; do
echo "querying server: ${server}"
ssh -n "${server}" -- "${command}"
done < $servers