前言
在生产环境中,尤其是集群之间,为了可能使机器之间免密拜访,通常须要配置ssh keys 互信,一两台机器手动也会很快解决好,但如果是多台机器,手动做互信就显得有点力不从心了,有可能还会配置谬误,为了可能一键化实现主动配置,以下应用shell脚本实现主动互信过程。
一、实现脚本
脚本名称为:autoexssh.sh,执行脚本前须要在各个服务器上安装expect包,能够应用yum形式装置,也能够应用源码编译装置,Depend On You 。
#!/bin/bash#################################################################### @Author: Shaohua ## @Date: 2021-03-31 14:40:32 ## @Last Modified by: Shaohua ## @Last Modified time: 2021-03-31 14:43:57 #################################################################### #Define Current Directory CURRENT_DIR=$(cd "$(dirname $0)";pwd)#Define Toplevel DirectoryTOPLEVEL_DIR=$(cd ${CURRENT_DIR}/..;pwd)IPLIST=$(cat ${CURRENT_DIR}/iplist)if [ $# -ne 2 ];then echo "Usage:sh $0 用户名 明码" exit 99fiUSER=$1PASSWD=$2rm -rf ${CURRENT_DIR}/ssh/rm -f authorized_keysfor ip in $IPLIST;do mkdir -p ${CURRENT_DIR}/ssh/${ip}#删除已存在的id_rsa.pub/usr/bin/expect <<EOF spawn ssh ${USER}@${ip} rm -f ~/.ssh/* expect { "*yes/no*" { send "yesr";exp_continue } "*password*" { send "${PASSWD}r";exp_continue} }#每台服务器生成 rsa 加密文件 spawn ssh ${USER}@${ip} ssh-keygen -t rsa expect { "*yes/no*" { send "yesr";exp_continue } "*password*" { send "${PASSWD}r";exp_continue} "Overwrite*" { send "yr";exp_continue} "Enter file in which to save the key*" { send "r"; exp_continue} "Enter passphrase*" { send "r";exp_continue } "Enter same passphrase again*" { send "r"; exp_continue } }#将每台服务器的id_rsa.pub文件搁置于独立的文件夹 spawn scp ${USER}@${ip}:~/.ssh/id_rsa.pub ${CURRENT_DIR}/ssh/${ip}/ expect { "*yes/no*" { send "yesr";exp_continue} "*password*" { send "${PASSWD}r";exp_continue} }EOFdone#将所有的 id_rsa.pub 搁置到 authorized_keys中for ip in $IPLIST;do cat ${CURRENT_DIR}/ssh/${ip}/id_rsa.pub >> ${CURRENT_DIR}/authorized_keys chmod 600 ${CURRENT_DIR}/authorized_keysdone#将 authorized_keys 放入到 每台服务器主机的 ~/.ssh/目录下for ip in $IPLIST;do/usr/bin/expect <<EOF spawn scp ${CURRENT_DIR}/authorized_keys ${ip}:~/.ssh/ expect { "*yes/no*" { send "yesr";exp_continue} "*password*" { send "${PASSWD}r";exp_continue} }EOFdone#所有服务器之间的相互连接防止提示符呈现for ip in $IPLIST;do/usr/bin/expect <<EOF spawn ssh ${ip} date expect { "*yes/no*" { send "yesr";exp_continue } "*password*" { send "${PASSWD}r";exp_continue} }EOFdone#生成 remote_ssh 供近程服务器能够执行相互拜访脚本,防止第一次连贯呈现手动交互的状况cat > ${CURRENT_DIR}/remote_ssh<<eofIPLIST="$(echo $(cat ${CURRENT_DIR}/iplist))"for ip in ${IPLIST} ;doUSER=$(whoami)/usr/bin/expect <<EOF spawn ssh ${USER}@${ip} date expect { "*yes/no*" { send "yesr";exp_continue} "*password*" { send "${PASSWD}r"; exp_continue} }EOFdoneeof#拷贝近程 remote_ssh 脚本,并在各节点近程执行for ip in $IPLIST;do scp ./remote_ssh $ip:~//usr/bin/expect <<EOF spawn ssh ${USER}@${ip} sh ~/remote_ssh expect { "*yes/no*" { send "yesr";exp_continue} "*password*" { send "${PASSWD}r"; exp_continue} }EOFdone
二、配置文件
配置文件为各个节点中的主机名称,因而,执行脚本之前须要将所有节点中的主机名解析hosts文件搁置在每个节点下的/etc/下,当然也能够间接应用ip地址。如下,有6台服务器:
host1host2host3host4host5host6
三、演示示例
这里有10台节点。
hosts 文件
[root@node01 ~]# tail -10 /etc/hosts10.10.20.70 node01.com node0110.10.20.71 node02.com node0210.10.20.72 node03.com node0310.10.20.73 node04.com node0410.10.20.74 node05.com node0510.10.20.75 node06.com node0610.10.20.76 node07.com node0710.10.20.77 node08.com node0810.10.20.78 node09.com node0910.10.20.79 node10.com node10
iplist 文件
[root@node01 autossh]# cat iplist node01node02node03node04node05node06node07node08node09node10
执行
[root@node01 autossh]# sh autoexssh.shUsage:sh autoexssh.sh 用户名 明码
指定用户和明码
[root@node01 autossh]# sh autoexssh.sh root redhat
输入局部如下:
结语
该脚本如果应用在 root 用户下能够任意执行,如果是普通用户执行,留神受权普通用户的执行权限和目录写入权限。脚本获取形式:
git clone https://github.com/DeveloperHonor/ssh-keys.git