Linux dsh 管理多台机器

最近接触到多台物理机操作,接触到了dsh,网上查了查,这里记录一下。

介绍

git地址:

https://github.com/fishjam/dsh

https://www.netfort.gr.jp/~dancer/software/dsh.html.en

      当手上一台机器的时后, 管理就登入管理, 两台的时后一样, 但是当手上有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 / Ubuntu安装

apt-get install dsh

在RHEL / CentOS / Fedora上源码安装:

wget http://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.9.tar.gz
tar zxvf libdshconfig-0.20.9.tar.gz
cd libdshconfig-0.20.9
./configure
make && make install
wget http://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.25.9.tar.gz
tar zxvf dsh-0.25.9.tar.gz
cd dsh-0.25.9
./configure
make && make install

32位系统:ln -s /usr/local/lib/libdshconfig.so.1 /lib/
64位系统:ln -s /usr/local/lib/libdshconfig.so.1 /lib64/

[root@localhost ~]# which dsh
/usr/local/bin/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

用法介绍:

[root@china-channel ~]# dsh –help
Distributed Shell / Dancer’s shell version 0.25.9
Copyright 2001-2005 Junichi Uekawa,
distributed under the terms and conditions of GPL version 2

-v –verbose                   Verbose output  详细模式输出
-q –quiet                     Quiet  安静模式输出
-M –show-machine-names      Prepend the host name on output显示被控制主机的hostname
-H –hide-machine-names      Do not prepend host name on output  不显示主机名(default)
-i –duplicate-input            Duplicate input given to dsh
-b –bufsize                   Change buffer size used in input duplication
-m –machine [machinename]     Execute on machine 主机名
-n –num-topology              How to divide the machines
-a –all              Execute on all machines  主机列表,默认列表在/root/.dsh/machines.list
-g –group [groupname]  Execute on group member 指定主机名组,主机名组在/root/.dsh/group/
-f –file [file]               Use the file as list of machines  选择主机列表
-r –remoteshell [shellname]   Execute using shell (rsh/ssh)  指定远程工具,默认为rsh
-o –remoteshellopt [option]   Option to give to shell
-h –help                      Give out this message
-w –wait-shell                Sequentially execute shell 指定顺序运行,默认是并行
-c –concurrent-shell          Execute shell concurrently
-F –forklimit [fork limit]    Concurrent with limit on number
-V –version                   Give out version information

 

更改shell类型

更改配置文件中的remoteshell为ssh:

remoteshell=rsh

改为

remoteshell=ssh

创建控制的主机列表

[root@china-channel ~]# cat /root/.dsh/machines.list
web@218.85.1.2
web@218.85.1.3
[root@china-channel ~]# cat /root/.dsh/group/umail 
user@218.85.1.2
user@218.85.1.3

 

使用范例

      dsh  -f  /root/.dsh/group/umail  'ls -al'

  • dsh -aw
  • 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"

配置ssh无密码登陆

[root@china-channel ~]# ssh-keygen -t rsa
回车三次。
在/root/.ssh目录下生成id_rsa,id_rsa.pub
把id_rsa.pub拷贝到被控制机的/root/.ssh/目录下:

[root@mail176 .ssh]#mv id_rsa.pub authorized_keys
[root@mail176 .ssh]#chmod 600 authorized_keys

对于有多个不同控制机的,可以通过cat id_rsa.pub >>authorized_keys 追加的形式

应用举例:

[root@china-channel AIMstor]# dsh -M -r ssh -a -- free
218.85.1.2:           total       used       free     shared    buffers     cached
218.85.1.2: Mem:       8168272    8138584      29688   0   197176    3516140
218.85.1.2: -/+ buffers/cache:    4425268    3743004
218.85.1.2: Swap:     16386260    1011732   15374528
218.85.1.3:           total       used       free     shared    buffers     cached
218.85.1.3: Mem:   8168272    8103304      64968      0    543324    3085280
218.85.1.3: -/+ buffers/cache:    4474700    3693572
218.85.1.3: Swap:     16386260    1039780   15346480


[root@china-channel AIMstor]# dsh -M -r ssh -a -- free|grep /cache
218.85.1.2: -/+ buffers/cache:    4423528    3744744
218.85.1.3: -/+ buffers/cache:    4462684    3705588

#############################

1、 dsh是通过SSH方式连接到服务器,所以需要在所有服务器上安装SSH。

2、 在srv.example.zqin上通过如下命令安装dsh。

srv:~# apt-get -y install libdshconfig1 libdshconfig1-dev dsh

3、 在使用dsh进行管理时,需要输入被管理服务器的用户名及密码,为了使用起来更加方便可使用如下命令将SSH的公钥复制到被管理服务器。

 srv:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
dd:e9:d3:84:fc:4c:ff:b4:b0:fa:12:fa:fd:49:3d:4d root@testsrv
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . o o |
| S . = o E|
| o * oo|
| . +.+o=|
| . ...+.=|
| .o++.+.|
+-----------------+
srv:~# scp ~/.ssh/id_rsa.pub 192.168.159.21:/root/.ssh/authorized_keys
srv:~# scp ~/.ssh/id_rsa.pub 192.168.159.22:/root/.ssh/authorized_keys
srv:~# scp ~/.ssh/id_rsa.pub 192.168.159.31:/root/.ssh/authorized_keys
srv:~# scp ~/.ssh/id_rsa.pub 192.168.159.32:/root/.ssh/authorized_keys

如果被管理的服务器比较多也可以编写个脚本来复制公钥。下面是一个复制公钥到多个服务器上的脚本。

for i in $(seq 200 253)
do
ssh 192.168.159.$i -C mkdir /root/.ssh
scp ~/.ssh/id_rsa.pub 192.168.1.$i:/root/.ssh/authorized_keys
done

4、 为了方便使用dsh,可以将所有被管理服务器分类并存放到对应文件中。

将所有被管理服务器的IP地址(或FQDN)加入$HOME/.dsh/machines.list文件中(每行一个)。

在$HOME/.dsh/group/目录下建立名为web的文件,并将web1、web2的IP地址(或FQDN)加入其中(每行一个)。

在$HOME/.dsh/group/目录下建立名为db的文件,并将db1、db2的IP地址(或FQDN)加入其中(每行一个)。

在上述配置完成后就可以在srv上通过dsh进行批量操作了,下面我们一起来看几个例子。

 

例子

1、 在db1.example.zqin上执行reboot命令。

srv:~# dsh -M -m db1.example.zqin -- reboot

2、 在$HOME/.dsh/machines.list文件中定义的所有服务器上同时执行updatedb命令。

srv:~# dsh -M -a -- updatedb

3、 在$HOME/.dsh/group/web文件中定义的所有服务上面上同时执行命令。

srv:~# dsh -M -g -- /etc/init.d/apache2 restart

 

相关文章:

https://my.oschina.net/phpython/blog/597771

http://blog.cheyo.net/268.html

https://www.howtoing.com/using-dsh-distributed-shell-to-run-linux-commands-across-multiple-machines

https://blog.csdn.net/li385805776/article/details/17030953

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值