管理多台機器的好工具 dsh (分散式 Shell)

當手上一台機器的時後, 管理就登入管理, 兩台的時後一樣, 但是當手上有 10台 或 上百台機器的時後, 該怎麼管理?

上百台機器, 遇到 機器掛掉 / 系統需要升級 / 要對所有機器做某個動作時, 該怎麼辦?

通常最常用的做法是: 使用 ssh domain_name ls (就可以遠端對 domain_name 下 ls 的 command), 由此寫個 for loop 跑 ssh domain_name command 等.

不過每次都要寫 for, 然後常常會遇到些意外狀況, 總之不是快速又保險的做法.

推薦使用工具 - DSH

DSH - dancer's shell / distributed shell

  • 官方解釋: dsh is an implementation of a wrapper for executing multiple remote shell (rsh/remsh/ssh) commands. rsh/remsh/ssh are wrappers for executing shell (ksh/bash/zsh/tcsh/sh .. etc... ) on remote hosts. 
  • MAN 解釋: dsh executes command remotely on several different machines at the same time. An utility to effectively do a for a in $(seq 1 10); do rsh $a command; done in bourne shell. 

Debian 安裝

apt-get install dsh

操作方式

dsh [-m machinename | -a | -g groupname] [-r remoteshellname] [-c | -w | -i | -F forklimit ] -- commandline

命令選項

  • -a (all)
    • /etc/dsh/machines.list | $(HOME)/.dsh/machines.list
  • -g group
    • /etc/dsh/group/groupname | $(HOME)/.dsh/group/groupname
  • -m [machinename[,machinename]*]
  • -r remoteshell shellname
  • 其它詳細選項可見 man dsh 或 dsh --help

使用範例

  • dsh -a w
  • dsh -r ssh -a -- w
  • dsh -r ssh -m mach1 -m mach2 -c -- 'echo $HOSTNAME $(cat /proc/loadavg)' 

相容寫法(範例)

  • dsh -m example.com,example2.com -c sudo apt-get update
  • dsh -m example.com -m example2.com -c sudo apt-get update
  • dsh -m example.com -m example2.com -c "sudo apt-get update"
  • dsh -m example.com -m example2.com -c -- "sudo apt-get update"

如何使用 Group 方式管理

  1. vim /etc/dsh/group/group_name
  2. 內容加入 domain name list
  3. 操作方式: dsh -g group_name -c "w"

其它相關網頁

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈管理多台機器的好工具 dsh (分散式 Shell)〉中有 3 則留言

  1. Dear Tsung:
    想請問您,您是否有順利將「dsh」安裝到其他版本 linux 的經驗嗎?
    小弟按照官方的安裝建議,安裝了「libdshconfig」→「dsh」。
    執行時,還是會出現找不到相關函式庫的情形,
    因此留言請教您,您有使用其他版本安裝嗎?
    或是..安裝完,有需要設定哪些設定才能使用嗎?
    麻煩您了,謝謝。

  2. 厄, 不好意思, 我有安裝到 Ubuntu/Debian 的經驗而已耶.
    您用的 Linux 版本是?
    裝好後, 不用做任何設定就可以直接使用了~ 🙂

  3. 目前使用的版本是 CentOS 5.2 的版本,
    不過,似乎用 tarball 安裝有點問題,
    沒有辦法順利編譯出 libdshconfig 。
    即使有找到 For CentOS_5 的 rpm 檔,仍然無法使用。@@~
    不過,沒關係,還是非常謝謝你的回答。 🙂

paul 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料