dsh:分布式 shell

dsh 简单说来就是可以同时通过 ssh 来控制多台机器。在一台机器上面安装,然后在需要控制的机器上添加上认证的公钥,建立了信任,然后在安装了dsh的机器上面用dsh来执行命令,就会在配置好的机器列表中全部都执行一遍, 再将结果返回回来

 

在 dsh 的配置文件里面可以把机器分组,然后在 dsh 的选项里面指定对那个组进行操作,比如

dsh -g www -c “w”

将对所有输入 www 组的机器执行 w 命令。

执行命令可以并行执行,也可以串行执行。并行执行是把命令同时发给所有机器,串行执行是一个一个机器的执行。等上一个机器执行完了再执行下一个机器。

这个命令对维护多个机器的同学应该有用。不用老是用 for 循环了。

 

官网:http://www.netfort.gr.jp/~dancer/software/dsh.html.en

 

对需要控制的服务器添加ssh认证信息的方法:

scp ~/.ssh/id_rsa.pub root@server:/root/.ssh/authorized_keys 

 

创建主机列表:

vi webapp.txt
192.168.56.11
192.168.56.12
192.168.56.13
192.168.56.14

配置SSH信任关系

mkdir ~/.ssh
ssh-keygen -t rsa
scp ~/.ssh/id_rsa.pub 192.168.56.11:/root/.ssh/authorized_keys
scp ~/.ssh/id_rsa.pub 192.168.56.12:/root/.ssh/authorized_keys
scp ~/.ssh/id_rsa.pub 192.168.56.13:/root/.ssh/authorized_keys
scp ~/.ssh/id_rsa.pub 192.168.56.14:/root/.ssh/authorized_keys

或者通过脚本批量拷贝

ip=192.168.56.
for i in $(seq 11 14)
do
ssh $ip$i -C mkdir /root/.ssh
scp ~/.ssh/id_rsa.pub $ip$i:/root/.ssh/authorized_keys
done

或者通过读取文件的脚步拷贝

for i in `cat webapp.txt`
do
ssh $i -C mkdir /root/.ssh
scp ~/.ssh/id_rsa.pub $i:/root/.ssh/authorized_keys
done

 

 dsh批量管理linux服务器

 
一,安装dsh
 
mkdir dsh
cd dsh
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
sudo make install
cd ..
 
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
sudo make install
 
ln -s /usr/local/lib/libdshconfig.so.1 /lib/
#reload lib path
/ sbin / ldconfig
 
[root@localhost ~]# which dsh
/usr/local/bin/dsh
 
二,用法介绍:
 
[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
三,创建控制的主机列表
 
[root@china-channel ~]# cat /root/.dsh/machines.list
218.85.1.2
218.85.1.3
[root@china-channel ~]# cat /root/.dsh/group/umail 
218.85.1.2
218.85.1.3
 
四,配置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
 
[root@china-channel AIMstor]# dsh -M -r ssh -a -- df -h          
218.85.1.2: Filesystem            Size  Used Avail Use% Mounted on
218.85.1.2: /dev/sda3              48G   31G   15G  67% /
218.85.1.2: /dev/sda6             792G  377G  375G  51% /bk
218.85.1.2: /dev/sda2              48G  4.7G   41G  11% /opt
218.85.1.2: /dev/sda1             251M   17M  222M   7% /boot
218.85.1.2: /dev/sdb1             2.7T 1008G  1.6T  39% /mail
218.85.1.2: tmpfs                 3.9G     0  3.9G   0% /dev/shm
218.85.1.3: Filesystem            Size  Used Avail Use% Mounted on
218.85.1.3: /dev/sda3              48G   35G   11G  77% /
218.85.1.3: /dev/sda6             792G  215G  537G  29% /bk
218.85.1.3: /dev/sda2              48G  5.7G   40G  13% /opt
218.85.1.3: /dev/sdb1             2.7T  812G  1.8T  32% /mail
218.85.1.3: /dev/sda1             251M   17M  222M   7% /boot
218.85.1.3: tmpfs                 3.9G     0  3.9G   0% /dev/shm
 
注:
dsh -M -r ssh -a --  如果需要执行的命令比较复杂的话,将后面的所有命令用单引号引起来,例如:
dsh -M -r ssh -a -- 'echo "abc" >> /root/test.txt'
 
 
------------------------------------------------------
 
dsh 的安装使用

dsh能帮助我们方便的管理多台主机,免去了我们写循环的麻烦,而且一些集群管理工具需要用到dsh。

在centOS5下安装

直接运行脚本

 

#!/bin/bash

# Build and install the library first
wget http://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.13.tar.gz
tar xzvf libdshconfig-0.20.13.tar.gz
cd libshconfig-0.20.13
./configure $ make
sudo make install
# Then build and install the utility
cd ..
wget http://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.25.9.tar.gz
tar xzvf dsh-0.25.9.tar.gz
cd dsh-0.25.9
./configure
make
sudo make install

#reload lib path
/sbin/ldconfig

然后建立一个群组

vi ~/.dsh/group/temp

server1

server2

server3

 
简单使用:

dsh -g temp --r ssh -- df -h

-g 制定group-a 全部主机默认只用~/.dsh/machines.list

-M是列出主机名-r是指定连接方式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值