ssh+rsync批量管理,批量分发

现在我简单架设了一个7台服务器的集群集体如下,架设集群的过程我就省略了…

[nfs存储一台]
192.168.42.10
[负载均衡2台]
192.168.42.40
192.168.42.41
[web服务器2台]
192.168.42.30
192.168.42.31
[备份1台]
192.168.42.20
[mysql 1台]
192.168.42.50

我现在需要批量管理这些服务器,刚开始用xshell一台,一台登录管理,觉得非常痛苦,后来为了方便学到了ssh+rsync 批量管理,现将技术分享一下:

具体思路 : 我用 nfs 存储做分发机,因为集群的所有的host文件,配置文件都需要统一,所以不可能一台一台复制,我是先将nfs的配置,做好,通过ssh+rsync技术实现批量管理,批量分发,其中涉及到三个主要脚本:exe_commond.sh(以root身份执行命令)fenfa.sh(以magedu身份执行命令),ip_hosts.sh(包含所有主机IP信息),为什么需要两个不同身份的脚本文件呢.听我详细介绍其中的奥秘:

1.我现在用xshell连接 分发机一台机器即可.因为像添加用户,设置密码,等超级权限还是得root去做 所有的集群root账号密码是一样的.因此一个脚本即可管理所有机器.

脚本代码:

#!/bin/bash

# 脚本用来批量创建集群用户,删除用户,分发公钥,执行命令等.
# 执行命令需要输入root密码,一次即可
# 命令参数: "commond" #要执行的命令
# 分发公钥参数: "fenfa" #即可
# email:626612631@qq.com
# function: remote dis ssh key.
# version:1.1
. /etc/init.d/functions

COMMOND=$1
SCRIPT_DIR="$( cd "$( dirname "$0"  )" && pwd  )"
IP_HOSTS_FILE="ip_hosts.sh"
MANUSER="magedu"
MANUSERPASS="123456"


[ $# -ne 1 ] && echo "Parameter is a command or str {fenfa}" && exit 2

declare -a IP_ARR

if [ ! -e ${SCRIPT_DIR}/${IP_HOSTS_FILE} ]; then
    echo -e  "current path missing \033[31m ${IP_HOSTS_FILE} \033[0m file"
    exit 2
fi

IP_ARR=(`grep -v -E "(^#)|(^$)" ${SCRIPT_DIR}/${IP_HOSTS_FILE} 2>/dev/null`)
if [ ${#IP_ARR[@]} -lt 0 ];then
   echo -e  "error reading file, please confirm IP format"
   exit 2
fi

function show_success(){
  action "Command execution" /bin/true
}

function exe_commond(){

 echo  -n  "please inut root passwd. " 
 read  -s  password
 echo " "

 for ip in ${IP_ARR[@]};do
    /usr/bin/expect -c "
    set timeout -1
    spawn /usr/bin/ssh root@${ip} ${COMMOND} 
    expect {
        \"*yes/no\" { send \"yes\r\"; exp_continue }
        \"*password:\" { send \"${password}\r\" }
    }
    expect eof" >/dev/null 2>&1 ;
    if [ $? -eq 0 ];then
           action "$ip: execute command successfully"   /bin/true
         else
           action "$ip: execute command fail"    /bin/false
    fi

 done


}


if [ "${COMMOND}" != 'fenfa' ];then
 exe_commond
 show_success
 exit 0
fi

#分发公钥
USERNA=`/usr/bin/whoami`

if [ "${USERNA}"=='root' ];then

  cd /home/magedu

elif [ "${USERNA}"=="${MANUSER}" ];then
  cd ~
else

  echo "Please distribute with ${MANUSER}  user"
  exit 3
fi


for fip in ${IP_ARR[@]};do
    /usr/bin/expect -c "
    set timeout -1
    spawn /usr/bin/ssh-copy-id -i  .ssh/id_dsa.pub   ${MANUSER}@${fip}
    expect {
        \"*yes/no\" { send \"yes\r\"; exp_continue }
        \"*password:\" { send \"${MANUSERPASS}\r\" }
    }
    expect eof" >/dev/null 2>&1 ;
    if [ $? -eq 0 ];then
           action "$fip: execute command successfully" /bin/true
         else
           action "$fip: execute command fail" /bin/false
    fi

done

show_success

脚本执行示例:
批量添加用户 magedu 添加这个用户的目的是用这个用户进行与交互,毕竟root用户权限太大了,而且用户密码也需要在脚本中保存,因此不说,各位都知道

[root@nfs-server script]# bash exec_commond.sh  "useradd magedu"
please inut root passwd.  
192.168.42.40: execute command successfully                [  OK  ]
192.168.42.41: execute command successfully                [  OK  ]
192.168.42.30: execute command successfully                [  OK  ]
192.168.42.31: execute command successfully                [  OK  ]
192.168.42.20: execute command successfully                [  OK  ]
192.168.42.50: execute command successfully                [  OK  ]
Command execution

批量设置magedu密码 ==设置的密码必须要和脚本中设置的一样==

[root@nfs-server script]# bash exec_commond.sh  "echo 123456 | passwd --stdin magedu"
please inut root passwd.  
192.168.42.40: execute command successfully                [  OK  ]
192.168.42.41: execute command successfully                [  OK  ]
192.168.42.30: execute command successfully                [  OK  ]
192.168.42.31: execute command successfully                [  OK  ]
192.168.42.20: execute command successfully                [  OK  ]
192.168.42.50: execute command successfully                [  OK  ]
Command execution

nfs分发也需要一个magedu账号,后面批量分发需要分发机的magedu和其他机器的magedu对应,为什么不把ip放进去一起执行呢,就怕执行其他的命令,导致分发机出错

useradd magedu
echo 123456 | passwd --stdin magedu

上面的步骤都做完以后,我们接下来创建密钥对,创建秘钥对,需要分发机进去magedu家目录执行:
一路回车即可
或者

ssh-keygen  -t dsa -P '' -f ~/.ssh/id_dsa &>/dev/null
[magedu@nfs-server ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/magedu/.ssh/id_dsa): 
Created directory '/home/magedu/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/magedu/.ssh/id_dsa.
Your public key has been saved in /home/magedu/.ssh/id_dsa.pub.
The key fingerprint is:
f2:18:c8:c0:db:bb:4c:38:77:4c:96:a4:6d:b4:dd:2d magedu@nfs-server
The key's randomart image is:
+--[ DSA 1024]----+
|                 |
| .               |
|  o   o          |
|   = * + . .     |
|  . = X S E .    |
|   . * =   .     |
|  o + + .        |
|   = o           |
|    o            |
+-----------------+

秘钥对创建完成以后,我们需要对所有机器分发公钥,目的就是为了分发机的magedu连接其他机器不需要再输入密码,自动完成分发任务

bash /script/exec_commond.sh "fenfa"
please inut root passwd.  
192.168.42.40: execute command successfully                [  OK  ]
192.168.42.41: execute command successfully                [  OK  ]
192.168.42.30: execute command successfully                [  OK  ]
192.168.42.31: execute command successfully                [  OK  ]
192.168.42.20: execute command successfully                [  OK  ]
192.168.42.50: execute command successfully                [  OK  ]
Command execution

至此分发公钥的任务完成了,现在我们就在分发机的magedu家目录下,创建文件a.txt
,利用分发脚本fenfa.sh分发a.txt试试

上脚本:

#!/bin/bash
# 用来分发文件和移动文件(rsync)
# email:626612631@qq.com
# function: remote dis ssh key.
# version:1.1
. /etc/init.d/functions

FILEPATH=$1
COMMOND=$2
SCRIPT_DIR="$( cd "$( dirname "$0"  )" && pwd  )"
IP_HOSTS_FILE="ip_hosts.sh"
MANUSER="magedu"

if [ "${FILEPATH}" == "--commond" ];then
   if [ $# -eq 1 ];then
        echo "Please enter a command to execute." 
        exit 2
   fi
else
   if [ ! -f ${FILEPATH} ]; then
          echo "File or directory does not exist". && exit 2
   fi
fi


if [ ! -e ${SCRIPT_DIR}/${IP_HOSTS_FILE} ]; then
    echo -e  "current path missing \033[31m ${IP_HOSTS_FILE} \033[0m file"
    exit 2
fi

IP_ARR=(`grep -v -E "(^#)|(^$)" ${SCRIPT_DIR}/${IP_HOSTS_FILE} 2>/dev/null`)
if [ ${#IP_ARR[@]} -lt 0 ];then
   echo -e  "error reading file, please confirm IP format"
   exit 2
fi

function  exec_fenfa(){
    expect -c "
    set timeout -1
    spawn $1
    expect {
        \"*yes/no\" { send \"yes\r\"; exp_continue }
    }
    expect eof" >/dev/null 2>&1;
    if [ $? -eq 0 ];then
           action "$2 is fenfa successfully" /bin/true
         else
           action "$2 is fenfa  fail" /bin/false
    fi

}

for ip in ${IP_ARR[@]};do
   if [ "${FILEPATH}" != "--commond" ];then
        #scp -r ${FILEPATH}  ${MANUSER}@${ip}:~  

        exec_fenfa  "scp -r ${FILEPATH}  ${MANUSER}@${ip}:~"  $ip  

   else
      #远程sudo 加-t  
      if [[ "${COMMOND}" =~ "sudo" ]]; then
        exec_fenfa  "ssh -t ${MANUSER}@${ip} ${COMMOND}" $ip
      else
        exec_fenfa  "ssh ${MANUSER}@${ip} ${COMMOND}"  $ip
      fi

   fi
done

示例:分发a.tx,连上其中一台的家目录,你就会看到文件已经在上面了

[magedu@nfs-server ~]$ bash /script/fenfa.sh a.txt
a.txt                              100%    0     0.0KB/s   00:00    
a.txt                              100%    0     0.0KB/s   00:00    
a.txt                              100%    0     0.0KB/s   00:00    
a.txt                              100%    0     0.0KB/s   00:00    
a.txt                              100%    0     0.0KB/s   00:00    
a.txt                              100%    0     0.0KB/s   00:00

但是有一点,如果我先把hosts文件分发到其他机器magedu的家目录下,但是需要把hosts文件copy到/etc/目录下,你会发现没有权限,更何况我们还需要远程将hosts文件拷贝到/etc/目录下,这个问题我采用rsync的功能,rsync具有本地复制的功能,而我们的其他机器没有装rsync怎么办呢,不着急,用下面的方法
so easy

bash /script/exec_commond.sh "yum install rsync -y" 

please inut root passwd.  

192.168.42.40: execute command successfully                [  OK  ]
192.168.42.41: execute command successfully                [  OK  ]
192.168.42.30: execute command successfully                [  OK  ]
192.168.42.31: execute command successfully                [  OK  ]
192.168.42.20: execute command successfully                [  OK  ]
192.168.42.50: execute command successfully                [  OK  ]
Command execution

然而我们装了rsync也不具备root权限,执行rsync /home/magedu/a.txt /etc/失败,那怎么办呢,别着急,有办法 利用sudo提权,怎么提权呢

bash /script/exec_commond.sh "echo 'magedu  ALL=(ALL)  NOPASSWD: /bin/rsync'>>/etc/sudoers"

please inut root passwd.  

192.168.42.40: execute command successfully                [  OK  ]
192.168.42.41: execute command successfully                [  OK  ]
192.168.42.30: execute command successfully                [  OK  ]
192.168.42.31: execute command successfully                [  OK  ]
192.168.42.20: execute command successfully                [  OK  ]
192.168.42.50: execute command successfully                [  OK  ]
Command execution

至此我们的工作都做完了.执行

[magedu@nfs-server ~]$ bash /script/fenfa.sh --commond "sudo rsync /home/magedu/a.txt /etc/"
192.168.42.40: execute command successfully [  OK  ]
192.168.42.41: execute command successfully [  OK  ]
192.168.42.30: execute command successfully [  OK  ]
192.168.42.31: execute command successfully [  OK  ]
192.168.42.20: execute command successfully [  OK  ]
192.168.42.50: execute command successfully [  OK  ]

完了以后,连接其他的服务器进去/etc/查看

[magedu@nginx-lib-1 etc]$ ls | grep a.txt
a.txt

注意一个问题ssh连接慢: 快速更改方法

sed -ir '13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no' sshd_config

文件顺利的被拷贝到/etc/目录下,后面分发其他文件是不是也很容易了呀,当然我写的脚本也有不完善的地方,自己根据自己的情况完善即可.

原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/73880

(1)
sraybansrayban
上一篇 2017-04-22 15:14
下一篇 2017-04-22 17:15

相关推荐

  • RPM包管理功能全解。【第三周】

    CentOS系统上rpm命令管理程序包: 安装、升级、卸载、查询、校验、数据库维护 rpm命令:rpm [OPTIONS] [PACKAGE_FILE]     安装:-i, –install     升级:-U, –update,&n…

    Linux干货 2016-11-26
  • 8.1_Linux管道的使用和用户管理

    什么是管道? 管道就是把命令1的标准输出发送给命令2的标准输入,把命令2发的标准输出发送给命令3的标准输入。。。 最后一个命令会在当前shell进程的子shell进程中执行用来 管道实现了使用目的单一的小程序,组合小程序完成复杂的任务 管道需要配合其他命令的使用 tr命令的使用,常常用于配合管道     -c或–co…

    Linux干货 2016-08-10
  • 基于Keepalived构建高可用集群配置实例(HA Cluster)

    什么是集群 简单的讲集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源。这些单个的计算机系统就是集群的节点(node)。一个理想的集群是,用户从来不会意识到集群系统底层的节点,在他/她们看来,集群是一个系统,而非多个计算机系统。并且集群系统的管理员可以随意增加和删改集群系统的节点。 关于更详细的高可用集群我们在后面再做详解…

    Linux干货 2016-11-03
  • N26-第六周博客作业

    请详细总结vim编辑器的使用并完成以下练习题 VIM有三种模式,分别为标准模式、输入模式、末行模式 模式切换:          标准模式——输入模式:i          标准模式——末行模式:: …

    Linux干货 2017-07-08
  • 进程管理和计划任务

    进程管理使用的工具以及命令,计划任务的创建和执行,以及工作中需要的注意事项

    2017-12-21
  • 简述计算机硬件组成,linux系统及简单命令详解

    1、计算机的硬件组成 (1)cpu(中央处理器):主要由运算器和控制器组成,它的运算器主要负责程序运算与逻辑判断,控制器则主要协调组件与各个设备之间工作。 (2)存储设备:存储设备又分为外存储设备,是用来存储数据信息的设备,它包括,硬盘,u盘,光盘,软盘。而其中内存(ram)和主板上的rom是属于内存储设备,内存是只读存储器,它用来暂时记录数据信息供给cpu…

    2017-07-09

评论列表(1条)

  • renjin
    renjin 2017-04-28 09:50

    主要介绍了ssh+rsync对主机的批量管理,内容写的很详细也比较超前,排版也非常好,继续努力