【实验需求】
1、配置三台主机之间基于ssh的key验证,host1、host2、host3之间互相进行远程访问时无需输入密码。
【实验环境】
host1: 192.168.119.129
host2: 192.168.119.146
host3: 192.168.119.150
【实验脚本】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #!/bin/bash
LOCAL_IP=`ip a show eth0 | awk -F'[ /]+' '/\<inet\>/{print $3}'` password=wuhaolam
HOSTLIST=' 192.168.119.146 192.168.119.150 '
if [ ! -e /root/.ssh/id_rsa ];then echo -e "\E[1;32m开始生成密钥对...\E[0m" ssh-keygen -P "" -f /root/.ssh/id_rsa &> /dev/null else echo -e "\E[1;34m密钥对已经存在\E[0m" fi
if ! rpm -q sshpass &> /dev/null;then yum -y install sshpass &> /dev/null || { echo -e '\E[1;31m"error: sshpass packet install false!"\E[0m'; exit; } fi
sshpass -p $password ssh-copy-id -o StrictHostKeyChecking=no $LOCAL_IP &> /dev/null
for IP in $HOSTLIST; do sshpass -p $password scp -o StrictHostKeyChecking=no -r /root/.ssh $IP:/root/ &> /dev/null echo $IP key_authentication already done. done
for IP in $HOSTLIST; do scp /root/.ssh/known_hosts $IP:/root/.ssh/ &> /dev/null done
echo -e "\E[1;32msuccessful\E[0m"
|