[脚本实例]——大批量建立信任关系+大批量分发文件

验证实验的环境
A:10.0.2.29    --->文件分发服务器  --->普通用户:user00
B:10.0.100.201 --->WEB1              --->普通用户:user00
C:10.0.100.202 --->WEB2              --->普通用户:user00


工具使用说明

                                               <工具使用说明>

一.用户使用说明

1.工具功能
  利用此工具脚本,可以使得管理服务器能够无密钥ssh登录到下属的全部服务器,进而实现大批量文件分发功能
  sshpass.sh脚本用于完成大批量部署信任关系
  Host.sh脚本用于提供用户界面并实现大批量文件分发
  
2.使用方法
请在管理服务器上完成以下操作:

(1)安装sshpass软件
   源码下载地址:http://sourceforge.net/projects/sshpass/ 
   # tar -xvf sshpass-1.05.tar.gz
   # ./configure && make && make install  

(2)创建user00用户并切换
   # useradd user00

(3)ssh相关设置
   # vim /etc/ssh/sshd_config
     PubkeyAuthentication yes //允许使用Key进行登录
   
   # service sshd restart

(4)生成公钥
   # su - user00
   $ ssh-keygen
   $ cd .ssh/
   $ cat id_rsa.pub > authorized_keys
   $ chmod 644 authorized_keys 
   $ ll authorized_keys 
     -rw-r--r-- 1 user00 user00 397 7月  27 20:08 authorized_keys

(5)把脚本和相关文件放入管理服务器中,运行


二.工具脚本说明

1.使用的文件
(1)webip.txt
   该文件按行存放着所有管理服务器管理的下属服务器IP
      
(2)pass.config
   该文件存放着所有管理服务器的下属服务器Root用户登录密码

2.全局变量说明

sshpass.sh

#!/bin/bash
#title:批量部署ssh信任关系
#author:Jelly_lyj
#date:2016-07-27
#version:v0.1

#===========================
#Set global variable
#===========================
PWDDIR=`pwd`
Iphost=`cat $PWDDIR/webip.txt`
Passwd=`cat $PWDDIR/pass.config`

RightLog="install_right.log"
Errorlog="install_error.log"
Username="user00"

#===========================
#Fuction--->Check_errlog
#===========================
Check_error()
{
if [ $? -ne 0 ]
then
    echo "`date +%Y-%m-%d-%H:%M:%S` $1 error" >$PWDDIR/install.log
    return 1
fi
}

#=======================================
#Function--->批量部署
#=======================================
Auto_ssh()
{
for ip in $Iphost
do
  #批量创建普通用户
  sshpass -p $Passwd ssh $ip StrictHostKeyChecking=no useradd $Username 2>/dev/null          
  Check_error useradd
     
  #批量在该用户家目录下建立.ssh文件
  sshpass -p $Passwd ssh $ip StrictHostKeyChecking=no mkdir /home/$Username/.ssh  
  Check_error mkdirssh
     
  #批量传输公钥
  sshpass -p $Passwd scp /home/user00/.ssh/authorized_keys  $ip:/home/$Username/.ssh  
  Check_error scpauthor
  
  #批量修改authorized_keys权限
  sshpass -p $Passwd ssh $ip StrictHostKeyChecking=no chmod 644 /home/$Username/.ssh/authorized_keys
  Check_error chmodauthor
  
  #批量修改authorized_keys属主属组
  sshpass -p $Passwd ssh $ip StrictHostKeyChecking=no chown $Username:$Username /home/$Username/.ssh/authorized_keys
  Check_error chownauthor
  
done
}

#=====================
#Fucntion--->主函数
#=====================
Main()
{
  Auto_ssh
  if [ $? -eq 0 ]
  then
     exit 1
  fi
}

#==================
#入口
#==================
Main

Host.sh

#!/bin/bash
#title:主菜单界面+远程登录与分发文件的功能函数
#author:Jelly_lyj
#date:2016-07-27
#version:v0.1

#=======================
#Set global variable
#=======================
WEB1="10.0.100.201"
WEB2="10.0.100.202"


#=======================
#Function--->界面菜单0
#=======================
Menu_0()
{
     echo -e "e[31;5m  * * * * * * * * * * * * * * * e[0m"
     echo -e "e[31;5m  *           主菜单界面      * e[0m"
     echo -e "e[31;5m  * * * * * * * * * * * * * * * e[0m"
     echo -e "e[1m          1. 登录到WEB服务器        e[0m"
     echo
     echo -e "e[1m          2. 文件大批量分发          e[0m"
     echo 
     echo -e "e[1m          0.     退出               e[0m"
     read -p "请选择:"  ch0
     case $ch0 in
     1) clear
        Menu_1
     ;;
     2) clear
        Menu_2
     ;;
     0) exit 1
     ;;
     *) echo "Enter Error"
        clear
        Menu_0
     ;;
     esac
     
}    

#=======================
#Function--->界面菜单1
#=======================
Menu_1()
{
     echo -e "e[31;5m  * * * * * * * * * * * * * * * * * *e[0m"
     echo -e "e[31;5m  *     请选择你要登录的WEB服务器   * e[0m"
     echo -e "e[31;5m  * * * * * * * * * * * * * * * * * *e[0m"
     echo 
     echo -e "e[1m             1. 登录 WEB1 服务器       e[0m"
     echo
     echo -e "e[1m             2. 登录 WEB2 服务器       e[0m"
     echo
     echo -e "e[1m             0. 返回主菜单             e[0m"

    read -p "请选择:"  ch1
    case $ch1 in
    1) Login_web1
    ;;
    2) Login_web2
    ;;
    0) clear
       Menu_0
    ;;
    *) echo "Enter Error"
       clear
       Menu_1
    ;;
    esac
}

#=======================
#Function--->界面菜单2
#=======================
Menu_2()
{
     echo -e "e[31;5m  * * * * * * * * * * * * * * * *e[0m"
     echo -e "e[31;5m  *    请根据提示选择相关操作  * e[0m"
     echo -e "e[31;5m  * * * * * * * * * * * * * * * *e[0m"
     echo
     echo -e "e[1m             1. 分发文件              e[0m"
     echo
     echo -e "e[1m             0. 返回主菜单             e[0m"
    
    read -p "请选择:"  ch2
    case $ch2 in 
    1) clear
       Distribute
    ;;
    0) clear
       Menu_0
    ;;
    *) echo "Enter Error"
       clear
       Menu_2
    ;;
    esac
}

#=======================
#Function--->login WEB1
#=======================
Login_web1()
{
    
    sshpass ssh -o StrictHostKeychecking=no $WEB1
}

#=======================
#Function--->login WEB2
#=======================
Login_web2()
{
    
    sshpass ssh -o StrictHostKeychecking=no $WEB2
}

#====================================
#Function--->Batch Distribute file
#====================================
Distribute()
{
    echo -e "e[1m请输入你想要批量分发到WEB的文件(绝对路径):e[0m" ; read filepath 
    echo
    echo -e "e[1m请输入你想要分发的位置:e[0m" ;read batchpath
    
    #文件/tmp/webip.txt记录web服务器的ip
    for ip in `cat /tmp/webip.txt`
    do
        sshpass scp $filepath $ip:$batchpath
    done   
    
}

#=======================
#Function--->主函数
#=======================
Main()
{
   clear
   Menu_0
}


#========================
#入口
#========================
Main
Host.sh
原文地址:https://www.cnblogs.com/snsdzjlz320/p/5730592.html