跳转至

服务器间免密登陆

ssh免密配置说明

一、安装基础组件

yum install -y expect

二、编写脚本

cat << FFF| tee ./ssh_withoutssh.sh
#!/bin/bash

[ ! -f $HOME/.ssh/id_rsa ] && ssh-keygen -t rsa -b 2048 -N "" -f $HOME/.ssh/id_rsa
read -s -p "请输入密码:  " passwd
cat $HOME/.ssh/id_rsa.pub >>$HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys
for ip in $(awk '/^[0-9]/{print $1}' IP.config); do
    #ssh-copy-id -o StrictHostKeyChecking=no -o CheckHostIP=no root@$ip
    /usr/bin/expect <<EOF
    spawn ssh-copy-id -o StrictHostKeyChecking=no -o CheckHostIP=no root@$ip 
    expect  {
        "yes/no" { send "yes\r";exp_continue }
        "password" { send "$passwd\r" }
    }
    expect eof
EOF
    # rsync -a $HOME/.ssh/id_rsa* $HOME/.ssh/authorized_keys -e 'ssh -o StrictHostKeyChecking=no -o CheckHostIP=no' root@$ip:/root/.ssh/
    let ret+=$?
done
FFF

三、创建需要免密的服务器IP清单

cat>>IP.config<<EOF
192.168.0.1
192.168.0.2
等需要免密的IP...
EOF

四、运行ssh_withoutssh.sh

sh ssh_withoutssh.sh